10. WRITE A MENU DRIVEN C PROGRAM TO IMPLEMENT THE
FOLLOWING USING RECURSION
A. FACTORIAL OF A NUMBER
B. FIBONACCI SERIES
SOURCE CODE:
int fact(int n){
if(n==1)
return 1;
return n*fact(n-1);
}
int fibo(int n){
if(n == 0 || n == 1)
return n;
return fibo(n-1) + fibo(n-2);
}
void main() {
int i,j=0,m,n,ch;
while(1){
m=0;
printf("\n1.FACTORIAL OF A NUMBER \n2. FIBONACCI SERIES
\n3.exit\n");
printf("Enter your choice :");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\nEnter n :");
scanf("%d",&n);
m=fact(n);
printf("factorial of a given number is :%d",m );
break;
case 2:
j=0;
printf("\nEnter n :");
scanf("%d",&n);
printf("fibonanci series of a given number is:");
for ( i = 0 ; i < n ; i++ ){
printf("%d\n", fibo(j));
j++;
}
break;
case 3:
exit(0);
default:
printf("Invalid option\n");
break;
}
}
}
1 comments:
good working sir it was amazing and very useful for MCA and BE students....thank you
Post a Comment