Like Us On Facebook
Follow Us On Twitter
Drop Down Menu
Wednesday, 24 April 2013
Inheritance And Its Types In C++
Saturday, 6 April 2013
C Program To Generate Fibonacci Series
Hi Friends,
Today, we'll learn how to generate a Fibonacci Series using C. The program and concept are pretty simple. But before that, let me tell you what exactly is a Fibonacci series. Fibonacci series is a special series in which the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. By going this way, the series generated will be:
Today, we'll learn how to generate a Fibonacci Series using C. The program and concept are pretty simple. But before that, let me tell you what exactly is a Fibonacci series. Fibonacci series is a special series in which the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. By going this way, the series generated will be:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 and so on...
The program is very simple.
First input is taken from the user that tells how many terms of the Fibonacci series will be generated and then as described above, a function is called in a loop to add the previous two numbers and generate the next number in the series.
First input is taken from the user that tells how many terms of the Fibonacci series will be generated and then as described above, a function is called in a loop to add the previous two numbers and generate the next number in the series.
Here is the C Code for Fibonacci Series:
/* Double-Click To Select Code */
#include<stdio.h>
#include<conio.h>
int fib(int n)
{
if(n==1||n==2)
return 1;
else
return(fib(n-1)+fib(n-2));
}
void main()
{
int i,n;
clrscr();
printf("Enter the number of terms: ");
scanf("%d",&n);
printf("\nThe Series is : \n\n0");
for(i=1;i<n;i++)
{
printf("%5d",fib(i)); // fib Function Called
}
getch();
}
Thursday, 28 March 2013
C Code To Check Whether A No. Is Palindrome Or Not
- Input number from user, 'num'.
- Apply Logic on 'num', produce reversed number 'rev'.
- If num == rev, print 'num' is Palindrome.
- Otherwise print 'num' is not Palindrome.
C Program To Check Whether A Number Is Palindrome Or Not:
/* Double-Click To Select Code */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf("Enter a number: ");
scanf("%d",&a);
b=a;
d=0;
while(a!=0)
{
d = d*10;
c = a%10;
a = a/10;
d = d+c;
}
if(d==b)
printf("\nThe number %d is plaindrome",b);
else
printf("\nThe number %d is not Palindrome",b);
getch();
}
Sunday, 17 March 2013
Selection Sort Algorithm And C Code
In computer science, a selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar Insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.
The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
Complexity:
| Worst case performance | О(n2) |
|---|---|
| Best case performance | О(n2) |
| Average case performance | О(n2) |
Step-by-step example:
Here is an example of this sort algorithm sorting five elements:
64 25 12 22 11
11 25 12 22 64
11 12 25 22 64
11 12 22 25 64
11 12 22 25 64
Selection sort can also be used on list structures that make add and remove efficient, such as a linked list. In this case it is more common to remove the minimum element from the remainder of the list, and then insert it at the end of the values sorted so far. For example:
64 25 12 22 11
11 64 25 12 22
11 12 64 25 22
11 12 22 64 25
11 12 22 25 64
Algorithm & Pseudo Code For Selection Sort:
/* Double-Click To Select Code */
/* a[0] to a[n-1] is the array to sort */
int i,j;
int iMin;
/* advance the position through the entire array */
/* (could do j < n-1 because single element is also min element) */
for (j = 0; j < n-1; j++) {
/* find the min element in the unsorted a[j .. n-1] */
/* assume the min is the first element */
iMin = j;
/* test against elements after j to find the smallest */
for ( i = j+1; i < n; i++) {
/* if this element is less, then it is the new minimum */
if (a[i] < a[iMin]) {
/* found new minimum; remember its index */
iMin = i;
}
}
/* iMin is the index of the minimum element. Swap it with the current position */
if ( iMin != j ) {
swap(a[j], a[iMin]);
}
}
C Program For Selection Sort:
/* Double-Click To Select Code */
#include<stdio.h>
#include<conio.h>
int smallest(int arr[],int k,int n);
void sort(int arr[],int n);
void main()
{
int arr[20],i,n,j,k;
clrscr();
printf("\nEnter the number of elements in the array: ");
scanf("%d",&n);
printf("\nEnter the elements of the array");
for(i=0 ; i < n ; i++)
{
printf("\n arr[%d] = ",i);
scanf("%d",&arr[i]);
}
sort(arr,n);
printf("\nThe sorted array is: \n");
for(i=0 ; i < n ; i++)
printf("%d\t",arr[i]);
getch();
}
int smallest(int arr[],int k,int n)
{
int pos=k,small=arr[k],i;
for(i=k+1;i<n;i++)
{
if(arr[i]<small)
{
small=arr[i];
pos=i;
}
}
return pos;
}
void sort(int arr[],int n)
{
int k,pos,temp;
for(k=0 ; k < n ; k++)
{
pos=smallest(arr,k,n);
temp=arr[k];
arr[k]=arr[pos];
arr[pos]=temp;
}
}
Output Of Program:
Please Comment If You Liked This Post !!
This article uses material from the Wikipedia article Selection_sort which is released under the Creative Commons Attribution-Share-Alike License 3.0
Insertion Sort Algorithm And C Code
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as Quicksort, heapsort, or Merge sort. However, insertion sort provides several advantages:
- Simple implementation
- Efficient for (quite) small data sets
- Adaptive (i.e., efficient) for data sets that are already substantially sorted: the time complexity is O(n + d), where d is the number of inversions
- More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
- Stable; i.e., does not change the relative order of elements with equal keys
- In-place; i.e., only requires a constant amount O(1) of additional memory space
- Online; i.e., can sort a list as it receives it
Step-by-step example:
The following table shows the steps for sorting the sequence {3, 7, 4, 9, 5, 2, 6, 1}. In each step, the item under consideration is underlined. The item that was moved (or left in place because it was biggest yet considered) in the previous step is shown in bold.
3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 5 7 9 2 6 1
2 3 4 5 7 9 6 1
2 3 4 5 6 7 9 1
1 2 3 4 5 6 7 9
Algorithm/Pseudo-Code:
for i ← 1 to i ← length(A)-1
{
//The values in A[ i ] are checked in-order, starting at the second one
// save A[i] to make a hole that will move as elements are shifted
// the value being checked will be inserted into the hole's final position
valueToInsert ← A[i]
holePos ← i
// keep moving the hole down until the value being checked is larger than
// what's just below the hole <!-- until A[holePos - 1] is <= item -->
while holePos > 0 and valueToInsert < A[holePos - 1]
{ //value to insert doesn't belong where the hole currently is, so shift
A[holePos] ← A[holePos - 1] //shift the larger value up
holePos ← holePos - 1 //move the hole position down
}
// hole is in the right position, so put value being checked into the hole
A[holePos] ← valueToInsert
}
Here is the C Code for Insertion Sort:
/* Double-Click To Select Code */
#include<stdio.h>
#include<conio.h>
void sort(int arr[],int n); //Function Prototype
void main()
{
int arr[20],i,n,j,k;
clrscr();
printf("\nEnter the number of elements in the array: ");
scanf("%d",&n);
printf("\nEnter the elements of the array");
for(i=0 ; i < n ; i++)
{
printf("\n arr[%d] = ",i);
scanf("%d",&arr[i]);
}
sort(arr,n);
printf("\nThe sorted array is: \n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
getch();
}
void sort(int arr[],int n)
{
int k,j,temp;
for(k=1 ; k < n ; k++)
{
temp=arr[k];
j=k-1;
while((temp < arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
}
Output:
This article uses material from the Wikipedia article Insertion_sort which is released under the Creative Commons Attribution-Share-Alike License 3.0
Saturday, 16 March 2013
Linear Search In An Array C Code
Here is the C Code for Linear Search:
/* Double-Click To Select Code */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],num,i,n,found=0,pos=-1;
clrscr();
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("\nEnter the elements: \n");
for(i=0;i<n;i++)
{
printf("arr[%d] = ",i);
scanf("%d",&arr[i]);
}
printf("\nEnter the number that has to be searched: ");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(arr[i]==num)
{
found=1;
pos=i;
printf("\n %d is found in the array at position = %d",num,i);
break;
}
}
if(found==0)
printf("\ %d does not exist in the array",num);
getch();
}
Output:
Please Comment If You Liked This Post !!
This article uses material from the Wikipedia article Linear Search which is released under the Creative Commons Attribution-Share-Alike License 3.0
Subscribe to:
Posts (Atom)







