You are on page 1of 2

1.

Sum of n natural numbers by using recursion


#include<stdio.h> #include<conio.h> int sum1(int ); void main() { int n,d; scanf("%d",&n); d=sum1(n); printf("\nsum is %d",d); getch(); } int sum1(int n) { if(n == 1) return 1; else return(n+sum1(n-1)); }

2. Factorial of a number using recursion.


#include<stdio.h> #include<conio.h> int fact(int ); void main() { int n,d; printf(Enter a number:); scanf("%d",&n); d=fact(n); printf("\nFactorial of a number is %d",d); getch(); } int sum1(int n) { if(n == 0) return 1; else return(n*fact(n-1)); }

3. to print the Fibonacci series by using recursion.


#include<stdio.h> #include<conio.h> int fib(int); void main() { int i,d,n; printf("Emter the number of terms:"); scanf("%d",&n); printf("\n Fibonacci Series is:\n"); for(i=0;i<=n;i++)

{ d= fib(i); printf(" %d",d); } getch(); } int fib(int i) { if(i==0) { return(0); } else if(i==1) { return(1); } else { return(fib(i-2)+fib(i-1)); } } OUTPUT Enter the number of terms: 7 0 1 1 2 3 5 8 13 (where 0 is oth term and 13 is 7th term)

You might also like