Vous êtes sur la page 1sur 9

Question:1 Write a program to generate Fibonacci Series, using recursion Answer: 1 #include<stdio.h> #include<conio.

h> int fib(int); int f=1,j=0,k=0,i=0,c; void main() { int l; clrscr(); printf("Enter limit\n"); scanf("%d",&l); f=0; printf("%d\n",f); f=1; printf("%d\n",f); for(c=0;c<l;c++) { f=fib(l); printf("%d\n",f); } getch(); } int fib(int n) { while(i<n) { if(i<=n) { i++; j=k; k=f; f=k+j; fib(1); return f; }

} } outputenter limit 4 0 1 1 2 3

Question: 2 write a program to calculate Factorial of nth number, using recursion Answer: 2 #include<stdio.h> int fact(int); int main(){ int num,f; printf("\nEnter a number: "); scanf("%d",&num); f=fact(num); printf("\nFactorial of %d is: %d",num,f); return 0; } int fact(int n){ if(n==1) return 1; else return(n*fact(n-1)); } Question: 3 write a program to implement Tower of Hanoi, using recursion. Answer: 3 #include #include void main() {

int *d,*s,i,n,dir; clrscr(); printf("Enter the number of disk\n"); scanf("%d",&n); dir=n&1; for(i=0;i<=n+1;i++) { d[i]=1;s[i]=i+1; } for(;;) { i=s[0]; if(i>n) break; printf("Move disk %d from tower%d to tower%d\n",i,d[i]=(d[i]+(i&1?dir:1-dir))%3+1,d[i]); s[0]=1; s[i-1]=s[i]; s[i]=i+1; } getch(); } Question: 4 write a program to calculate GCD of two numbers, using recursion Answer: 4 #include <stdio.h> void main () { int a, b, iGcd; unsigned Gcd( unsigned, unsigned ); printf( "Enter any 2 numbers whose GCD is to be found:"); scanf( "%d %d", &a, &b ); iGcd = Gcd( a, b); printf( " GCD of %d and %d is %d\n", a, b, iGcd ); } unsigned Gcd( unsigned m, unsigned n ) { if( n > m) { return Gcd( n, m ); } if( 0 == n ) { return m;

} else { return Gcd( n, m % n ); } } Output: Enter any 2 numbers whose GCD is to be found:252 105 GCD of 252 and 105 is 21

Question: 5 write a program to calculate power of a number, using recursion Answer: 5 #include<stdio.h> int main(){ int pow,num; long int res; long int power(int,int); printf("\nEnter a number: "); scanf("%d",&num); printf("\nEnter power: "); scanf("%d",&pow); res=power(num,pow); printf("\n%d to the power %d is: %ld",num,pow,res); return 0; } int i=1; long int sum=1; long int power(int num,int pow){ if(i<=pow){ sum=sum*num; power(num,pow-1); } else return sum; }

Question: 6 write a program to calculate power of a number, using recursion. Answer: 6 # include <stdio.h> # include <conio.h> /* Function to swap values at two pointers */ void swap (char *x, char *y)

{ char temp; temp = *x; *x = *y; *y = temp; } /* Function to print permutations of string This function takes three parameters: 1. String 2. Starting index of the string 3. Ending index of the string. */ void permute(char *a, int i, int n) { int j; if (i == n) printf("%s\n", a); else { for (j = i; j <= n; j++) { swap((a+i), (a+j)); permute(a, i+1, n); swap((a+i), (a+j)); //backtrack } } } /* Driver program to test above functions */ int main() { char a[] = "ABC"; permute(a, 0, 2); getchar(); return 0; }

Question: 7 write a program to swap two elements without using third variable. Answer: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b;

cout<<"Enter a: "; cin>>a; cout<<"Enter b: "; cin>>b; //Coding by: Snehil Khanor //http://WapCPP.blogspot.com cout<<"Before swapping:"<<endl; cout<<"a: "<<a<<" b: "<<b<<endl; a=a+b; b=a-b; a=a-b; cout<<"After swapping:"<<endl; cout<<"a: "<<a<<" b: "<<b; getch(); } Question: 8 write a program to remove all the duplicate elements present in the given array Answer: 8 #include<stdio.h> int main(){ int arr[50]; int *p; int i,j,k,size,n; printf("\nEnter size of the array: "); scanf("%d",&n); printf("\nEnter %d elements into the array: ",n); for(i=0;i<n;i++) scanf("%d",&arr[i]); size=n; p=arr; for(i=0;i<size;i++){ for(j=0;j<size;j++){ if(i==j){ continue; } else if(*(p+i)==*(p+j)){ k=j; size--; while(k < size){ *(p+k)=*(p+k+1); k++; } j=0; } }

} printf("\nThe array after removing duplicates is: "); for(i=0;i < size;i++){ printf(" %d",arr[i]); } return 0; }

Question: 9 Write a program to search an element using Linear Search Answer: 9 #include<stdio.h> int main(){ int a[10],i,n,m,c=0; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: "); for(i=0;i<=n-1;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); for(i=0;i<=n-1;i++){ if(a[i]==m){ c=1; break; } } if(c==0) printf("The number is not in the list"); else printf("The number is found"); return 0; } Question: 10 write a program to search an element using Binary Search Answer: 10 #include<stdio.h> #include<conio.h> void main() {

int array[10]; int i, j, n, temp, num; int low,mid,high; clrscr(); printf("Enter the value of the array\t"); scanf("%d",&n); printf("Enter the elements one by one:\n"); for(i=0;i<n;i++) { scanf("%d",&array[i]); } printf("Input array elements\n"); for(i=0;i<n;i++) { printf("%d\n",array[i]); } for(i=0;i<n;i++) { for(j=0;j<(n-i-1);j++) { if(array[j]>array[j+1]) { temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } printf("Sorted array is...\n"); for(i=0;i<n;i++) { printf("%d\n",array[i]); } printf("Enter the element to be searched\n"); scanf("%d",&num); low=1; high=n; do { mid=(low+high)/2; if(num<array[mid]) high=mid-1; else if(num>array[mid]) low=mid+1; } while(num!=array[mid] && low<=high); if(num==array[mid])

{ printf("\n\tis present at position %d",array[i],i+1); } else { printf("Search is FAILED\n"); } getch(); }

Vous aimerez peut-être aussi