instead of using loops.

#include<stdio.h>

main(){inta, fact;

printf("\nEnter any number: ");

scanf ("%d", &a);

fact=rec (a);

printf("\nFactorial Value = %d", fact);}

rec (intx){intf;if(x==1)return(1);else

f=x*rec(x-1);return(f);}

skip to main |
skip to sidebar
## Friday, February 23, 2007

###
C Program to Calculate Factorial of a number using Recursion

## Thursday, January 18, 2007

###
Generation of Fibonacci Sequence using Recursion.

Beginner's C Programming Blog

Write a program to calculate the factorial of a number. Use the concept of recursion

instead of using loops.

instead of using loops.

#include<stdio.h>

main(){inta, fact;

printf("\nEnter any number: ");

scanf ("%d", &a);

fact=rec (a);

printf("\nFactorial Value = %d", fact);}

rec (intx){intf;if(x==1)return(1);else

f=x*rec(x-1);return(f);}

Write a recursive function to obtain the first 25 numbers of a Fibonacci

sequence. In a Fibonacci sequence the sum of two successive terms gives the

third term. Following are the first few terms of the Fibonacci sequence:

1 1 2 3 5 8 13 21 34 55 89 ...

sequence. In a Fibonacci sequence the sum of two successive terms gives the

third term. Following are the first few terms of the Fibonacci sequence:

1 1 2 3 5 8 13 21 34 55 89 ...

#include<stdio.h>

main(){staticintprev_number=0, number=1; //static: so value is not lostintfibonacci (intprev_number,intnumber);

printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity

fibonacci (prev_number,number);}

fibonacci (intprev_number,intnumber){staticinti=1; //i is not 0, cuz 1 is already counted in main.intfibo;if(i==25){

printf ("\ndone"); //stop after 25 numbers}else{

fibo=prev_number+number;

prev_number=number; //important steps

number=fibo;

printf ("\n%d", fibo);

i++; // increment counter

fibonacci (prev_number,number); //recursion}}

Subscribe to:
Posts (Atom)