Sie sind auf Seite 1von 18

AIM : Program to find Factorial of a Number by using Recursion #include<stdio.

h> int main() { int n,f; int fact(int); printf("Enter a number : "); scanf("%d",&n); f=fact(n); printf("factorial = %d",f ); } int fact(int n) { int i=1,f=1; while(i<=n) { f=f*i; i++; } return f; }

OUTPUT : Enter a number : 5 factorial = 120

AIM : Program to find GCD of two number by using Recursion #include<stdio.h> int main() { int a,b; int gcd(int,int); printf("Enter two numbers :"); scanf("%d%d",&a,&b); printf("GCD = %d",gcd(a,b)); } int gcd(a,b) { if(b>a) { return gcd(b,a); } if(b==0) { return a; } else { return gcd(b,a%b); } } OUTPUT: Enter two numbers : 386 324 GCD = 2

AIM : Program to find nth Fibonacci number by using Recursion #include<stdio.h> int main() { int n,f; int fib(int); printf("Enter n value :"); scanf("%d",&n); f=fib(n); printf("%d Fibonacci number is %d",n,f); } int fib(n) { if(n==1) { return 0; } if(n==2) { return 1; } else { return (fib(n-1)+fib(n-2)); } }

OUTPUT : Enter n value : 5 2 fibonacci number is 3

AIM : Program to trace Towers of Honoi #include<stdio.h> void main() { int n; printf("Enter n Value : "); scanf("%d",&n); toh(n,'s','d','i'); } void toh(int n,char s,char d,char i) { if(n==1) { printf("\n Move %d disc from %c to %c",n,s,d); } else { toh(n-1,s,i,d); printf("\n Move %d disc from %c to %c",n,s,d); toh(n-1,i,d,s); } } OUTPUT : Enter n Value : 3 Move 3 disc from s to i Move 2 disc from s to i Move 1 disc from s to d Move 2 disc from i to d Move 3 disc from i to d

AIM : Program to Find an element in an Array by using Linear Search #include<stdio.h> int main() { int a[20],n,key,i,pos; int ls(int[],int,int); printf("Enter n value : "); scanf("%d",&n); printf("Enter array elements : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Enter key element : "); scanf("%d",&key); pos=ls(a,n,key); if(pos<0) { printf("Element fot found"); } else { printf("Element found at the position %d ",pos); } } int ls(int a[],int n,int key) { int i; for(i=0;i<n;i++) { if(key==a[i]) { return (i+1); break; } } if(i==n) { return -1; } } OUTPUT : Enter array size : 5 Enter elements : 1 2 3 6 5 Enter Key : 6 Element found at position : 4

AIM : Program to Find an element in an Array by using Binary Search

#include<stdio.h> int bs(int[],int,int,int); int main() { int a[20],n,i,key,low,high,pos; printf("Enter array size : "); scanf("%d",&n); printf("Enter elements into array : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Enter key : "); scanf("%d",&key); low=0; high=n; pos=bs(a,low,high,key); if(pos==-1) { printf("Element not found"); } else { printf("Element Found at position %d",pos); } } int bs(int a[],int low,int high,int key) { int mid; if(low>high) { return -1; } else { mid=(low+high)/2; if(key==a[mid]) { return mid+1; } else if(key>a[mid]) { low=mid+1; return bs(a,low,high,key); }

else {

high=mid-1; return bs(a,low,high,key); } } } OUTPUT : Enter array size : 5 Enter elements into array : 11 22 33 44 55 Enter key : 22 Element Found at position : 2

AIM : Program to Find an element in an Array by using Fibonacci Search #include<stdio.h> int fib(int); int fs(int[],int,int); int main() { int a[20],n,i,key,pos; printf("Enter array size : "); scanf("%d",&n); printf("Enter elements : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Enter Key : "); scanf("%d",&key); pos=fs(a,n,key); if(pos==-1) { printf("Element not found"); } else { printf("Element found at position : %d",pos); } return 0; } int fib(int n) { if(n==0) { return 0; } else if(n==1) { return 1; } else { return (fib(n-1)+fib(n-2)); } } int fs(int a[],int n,int key) { int m,p,q,j,temp; for(j=0;fib(j)<n;j++); j--; m=fib(j); p=fib(j-1); q=fib(j-2);

if(a[0]==key) { return 1; } while(1) { if(a[m]==key) { return m+1; } else if(key<a[m]) { if(q==0) return -1; else { m=m-q; temp=q; p=q; q=temp-q; } } else { if(p==1) { return -1; } else { m=m+q; p=p-q; q=q-p; } } } }

OUTPUT : Enter array size : 6 Enter elements : 5 6 7 8 9 10 Enter Key : 12 Element not found

AIM : Program to Sorting an Array elements by using Bubble Sort

#include<stdio.h> int main() { int a[10],n,i; int bs(int [],int); printf("Enter number of elments : "); scanf("%d",&n); printf("Enter elements into the array : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } bs(a,n); } int bs(int a[],int n) { int i,j,temp=0; for(i=0;i<n-1;i++) { for(j=0;j<n-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("The Sorted Array elements are :\t"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } return 0; } OUTPUT : Enter number of elments : 5 Enter elements into the array : 9 5 1 4 3 The Sorted Array elements are : 1 3 4 5 7

AIM : Program to Sorting an Array elements by using Selection Sort #include<stdio.h> int main() { int a[10],n,i; int ss(int [],int); printf("Enter number of elments : "); scanf("%d",&n); printf("Enter elements into the array : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } ss(a,n); } int ss(int a[],int n) { int i,j,sp,temp=0; for(i=0;i<n-1;i++) { sp=i; for(j=i+1;j<n;j++) { if(a[j]<a[sp]) { sp=j; } } temp=a[i]; a[i]=a[sp]; a[sp]=temp; } printf("After sortint array elements are : \n"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } return 0; } OUTPUT : Enter number of elments : 5 Enter elements into the array : 6 12 88 44 3 After Sorting array elements are : 3 6 12 44 88

AIM : Program to Sorting an Array elements by using Insertion Sort #include<stdio.h> int main() { int a[10],n,i; int is(int [],int); printf("Enter number of elments : "); scanf("%d",&n); printf("Enter elements into the array : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } is(a,n); } int is(int a[],int n) { int i,j,temp=0; for(i=0;i<n-1;i++) { j=i+1; temp=a[j]; while((j>0)&&(a[j-1]>temp)) { a[j]=a[j-1]; j--; } a[j]=temp; } printf("After sorting array elements are :\t"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } return 0; } OUTPUT : Enter number of elments : 5 Enter elements into the array : 69 84 11 22 99 After Sorting array elements are :11 22 69 84 99

AIM : Program to Sorting an Array elements by using Quick Sort #include<stdio.h> int main() { int a[10],n,i; int qs(int [],int,int); printf("Enter number of elments : "); scanf("%d",&n); printf("Enter elements into the array : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } qs(a,0,n-1); printf(After Sorting array elements are :\t ); for(i=0;i<n;i++) { printf("%d\t",a[i]); } return 0; } int qs(int a[],int low,int high) { int pos,i,j,temp; if(low<high) { pos=low; i=low; j=high+1; while(1) { i++; j--; while((a[i]<a[pos])&&(i<=j)) i++; while((a[j]>a[pos])&&(j>i)) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } if(i>j) { temp=a[j]; a[j]=a[pos]; a[pos]=temp; break;

} } qs(a,low,j-1); qs(a,j+1,high); } return 0; } OUTPUT : Enter number of elments : 5 Enter elements into the array : 3 4 2 5 6 After Sorting array elements are : 2 3 4 5 6

AIM : Program to Sorting an Array elements by using Radix Sort #include<stdio.h> #include<math.h> int main() { int a[10],n,i; int rs(int [],int); int pw(int,int); printf("Enter number of elments : "); scanf("%d",&n); printf("Enter elements into the array : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } rs(a,n); } int rs(int a[],int n) { int b[10][20],big,digits=0,pointer[10],i,j,k,l,p,r,rem,point; big=a[0]; for(i=0;i<n;i++) { if(a[i]>big) { big=a[i]; } } while(big>0) { digits++; big=big/10; } for(i=0;i<digits;i++) { for(p=0;p<10;p++) { pointer[p]=0; } for(j=0;j<n;j++) { rem=(a[j]/pw(10,i))%10; point=pointer[rem]; b[rem][point]=a[j]; pointer[rem]++; } l=0; for(k=0;k<10;k++) {

for(r=0;r<pointer[k];r++) { a[l]=b[k][r]; l++; } } } for(i=0;i<n;i++) { printf("%d\t",a[i]); } } int pw(int x,int n) { int i,power=1; for(i=1;i<=n;i++) power=power*1; return(power); } OUTPUT : Enter number of elments : 5 Enter elements into the array : 9 5 4 7 2 After Sorting array elements are : 2 4 5 7 9

AIM : Program to Sorting an Array elements by using Merge Sort #include<stdio.h> int ms(int [],int,int); int sort(int [],int,int,int); int main() { int a[20],n,i; printf("Enter number of elements :"); scanf("%d",&n); printf("Enter elements into array:"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } ms(a,0,n-1); printf("After sorting arrary elements are :\t"); for(i=0;i<n;i++) printf("%d ",a[i]); return 0; } int ms(int a[],int l,int h) { int mid; if(l<h) { mid=(l+h)/2; ms(a,l,mid); ms(a,mid+1,h); sort(a,l,mid,h); } return 0; } int sort(int a[],int l,int mid,int h) { int i,j,temp[20],k=0; i=l; j=mid+1; if(l<h) { while(i<=mid&&j<=h) { if(a[i]<a[j]) { temp[k]=a[i]; i++; k++; } else { temp[k]=a[j]; j++;

k++; } } while(i<=mid) { temp[k]=a[i]; i++; k++; } while(j<=h) { temp[k]=a[j]; j++; k++; } } k=0; for(i=l;i<=h;i++) { a[i]=temp[k]; k++; } return 0; }

OUTPUT : Enter number of elments : 6 Enter elements into the array : 95 62 32 14 89 99 After Sorting array elements are : 14 32 62 89 95 99

Das könnte Ihnen auch gefallen