C-Codes using Recursion
1. Write a program to calculate the factorial of the number using recursion
#include<stdio.h>
long factorial(int a);
int main()
{
int n;
long Fact;
printf("Enter the number:");
scanf("%d",&n);
Fact=factorial(n);
printf("The factorial is %ld",Fact);
}
long factorial(int n)
{
long f;
if(n==0)
{
return 1;
}
else
{
f=n*factorial(n-1);
return f;
}
}
2. Write a program to calculate the fibonacci sequence using recursion
#include<stdio.h>
int fibonacci(int);
int main()
{
int n,i=1;
printf("Enter the number of terms:");
scanf("%d",&n);
while(i<=n)
{
printf("\t%d",fibonacci(i));
i++;
}
return 0;
}
int fibonacci(int n)
{
if(n==1)
return 0;
else if (n==2)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}
3. Write a program to read a number n from user and calculate sum of first n natural numbers using Recursion
#include<stdio.h>
int sum(int);
int main()
{
int a,Sum;
printf("Enter the number:");
scanf("%d",&a);
Sum=sum(a);
printf("The sum of natural number upto %d is %d",a,Sum);
return 0;
}
int sum(int a)
{
int SUM=0;
if(a==0)
return 0;
else
SUM=a+sum(a-1);
return SUM;
}
4. Write a program to generate particular term in Fibonacci Series using Recursion
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n,i=1;
printf("Enter the number of terms:");
scanf("%d",&n);
printf("%d",fibonacci(n));
return 0;
}
int fibonacci(int n)
{
if(n==1)
return 0;
else if (n==2)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}
5. Write a program that will evaluate a floating-point number to be raised to an integer power using Recursion(i.e. y = m^n)
#include<stdio.h>
float power(float,float);
int main()
{
float m,n,pow;
printf("Enter the base,m and exponent,n:");
scanf("%f%f",&m,&n);
pow=power(m,n);
printf("The power is %.2f",pow);
return 0;
}
float power(float base, float exp)
{
if(exp==0)
return 1;
else
return (base*power(base,exp-1));
}
#include<stdio.h>
int reverse(int,int);
int main()
{
int n;
printf("Enter the positive number:");
scanf("%d",&n);
printf("The reversed number is %d",reverse(n,0));
return 0;
}
int reverse(int n,int result)
{
int R;
if(n>0)
{
R=n%10;
result=result*10+R;
reverse(n/10,result);
}
else
return result;
}
#include<stdio.h>
int sum(int,int);
int main()
{
int n,SUM;
printf("Enter the number:");
scanf("%d",&n);
SUM=sum(n,0);
printf("The sum of digits is %d",SUM);
}
int sum(int n,int S)
{
int R;
if(n!=0)
{
R=n%10;
S=S+R;
sum(n/10,S);
}
else
return S;
}
#include<stdio.h>
int prime(int n,int i)
{
int a=1,c=0;
if(i==1)
{
return 1;
}
else if(n%i==0)
{
return 0;
}
else
{
prime(n,i-1);
}
}
int main()
{
int n,c;
printf("Enter a num\n");
scanf("%d",&n);
c = prime(n,n-1);
if(c==1)
printf("It is prime.");
else
printf("It is composite.");
}
9. Write a program to display the prime numbers entered by the user in the range using the concept of Recursion.
#include<stdio.h>
int prime(int n,int i)
{
int a=1,c=0;
if(i==1)
{
return 1;
}
else if(n%i==0)
{
return 0;
}
else
{
prime(n,i-1);
}
}
int main()
{
int n1,n2,c,j;
printf("Enter a number range \n");
scanf("%d%d",&n1,&n2);
for(j=n1;j<=n2;j++)
{
c = prime(j,j-1);
if(c==1)
printf("%d ",j);
}
}
10. Write a program to check whether a number is Armstrong or not using the concept of Recursive Function.
#include<stdio.h>
int Armstrong(int,int);
int main()
{
int n,num,Result;
printf("Enter the number:");
scanf("%d",&n);
num=n;
Result=Armstrong(n,0);
if(Result==num)
printf("Armstrong Number");
else
printf("Not a Armstrong Number");
return 0;
}
int Armstrong(int n,int sum)
{
int R;
if(n!=0)
{
R=n%10;
sum=sum+R*R*R;
Armstrong(n/10,sum);
}
else
return sum;
}
11. Write a program to display the Armstrong number in the range using Recursion.
#include<stdio.h>
int main()
{
int n1,n2,result,num,R,i;
printf("Enter the no:");
scanf("%d%d",&n1,&n2);
printf("Armstrong number between %d and %d are:",n1,n2);
for(i=n1; i<=n2; i++)
{
num=i;
result=Armstrong(i,0);
if(result==num)
printf("%d\t",i);
}
}
int Armstrong(int n,int sum)
{
int R;
if(n!=0)
{
R=n%10;
sum=sum+R*R*R;
Armstrong(n/10,sum);
}
else
return sum;
}
12. Write a program to check whether a given number is Palindrome or not using the concept of Recursive Function.
#include<stdio.h>
int Palindrome(int, int);
int main()
{
int n,Result,num;
printf("Enter the number:");
scanf("%d",&n);
num=n;
Result=Palindrome(n,0);
if(num==Result)
printf("Palindrome Number");
else
printf("Not a palindrome number");
return 0;
}
int Palindrome(int n, int sum)
{
int R;
if(n!=0)
{
R=n%10;
sum=sum*10+R;
Palindrome(n/10,sum);
}
else
return sum;
}
13. Write a program to find out whether the nth term of the Fibonacci series is a prime or not. Read the value of n from the user and display the result in the main function. User separate user defined functions to generate the nth Fibonacci term and to check whether a number is prime or not.
#include<stdio.h>
int prime(int n,int i)
{
int a=1,c=0;
if(i==1)
{
return 1;
}
else if(n%i==0)
{
return 0;
}
else
{
prime(n,i-1);
}
}
int series(int n)
{
int fib;
if(n==1)
{
return 0;
}
else if(n==2)
{
return 1;
}
else
{
return series(n-1)+series(n-2);
}
}
int main()
{
int n,term,check;
printf("Enter a num\n");
scanf("%d",&n);
term = series(n);
check = prime(term,term-1);
if(check==1)
printf("%dth term %d is prime.",n,term);
else
printf("%dth term %d is not prime.",n,term);
}
14. Write a program to calculate the sum of series given below using Recursive Function. 1 + 11 + 111 + … + up to N terms. If N is read as 5, the series is: 1 + 11 + 111 + 1111 + 11111.
#include<stdio.h>
int Sum(int);
int main()
{
int n;
printf("Enter the value of n:");
scanf("%d",&n);
printf("The sum of the series is %d",Sum(n));
return 0;
}
int Sum(int n)
{
int sum=0,i;
if(n==1)
return 1;
else
{
sum=0;
for(i=0; i<n; i++)
{
sum=sum+pow(10,i);
}
return sum+Sum(n-1);
}
}
Comments
Post a Comment