Write a program to find out the summation of n numbers using recursion.

#include<stdio.h>
#include<conio.h>

void main()
{
int n;
int getsum(int);
clrscr();

printf("Enter the value of n\n");
scanf("%d",&n);

if(n<0)      //to check if the number is a non-negative integer
  printf("Invalid value of n\n");
else
  printf("Sum = %d\n",getsum(n)); //function call to getsum() passing n as parameter

getch();
}

int getsum(int n)
{
  if(n>0)
    return n+getsum(n-1);
    /* recursive call to getsum() passing n-1 as parameter to keep on
      adding all numbers till 1 is reached */

  else
    return 0;
}

 

Write a program using recursive function power to compute xn power(x,n) = 1 if n=0 power(x,n) = x if n=1 power(x,n) = x*power(x,n-1) otherwise

#include<stdio.h>
#include<conio.h>

void main()
{
float x;
int n;
float power(float,int);
clrscr();
printf("Enter the base\n");
scanf("%f",&x);
printf("Enter the power\n");
scanf("%d",&n);
if(n<0)      //to check if power is a non-negative integer
  printf("Invalid power value\n");
else
  if(x==0)
    if(n==0)
      printf("Answer not defined\n"); //base and power both zero then answer is not defined
    else
      printf("Answer = 0\n"); //base=0 and power is positive then answer is 0
  else
    printf("Answer = %.2f\n",power(x,n)); //base not equal to 0 then call the recursive function power() passing float x and int y as parameters

getch();
}

float power(float x,int n)
{
  if(n==0)
    return 1;
    /* if power=0 then answer=1...hence return 1 */
  else
    if(n==1)
      return x;
      /* when power=1 the answer of base x to the power 1 is the
      base itself...hence return x */
    else
      return x*power(x,n-1);
      /* if power is greater than 1 then answer is x* recursive call to
      the function power() passing base 'x' and power one less i.e. 'n-1'
      as parameters */
}

 

Write a recursive function that accepts a positive integer and prints the reverse.

#include<stdio.h>
#include<conio.h>

void main()
{
int num;
void reverse(int);
clrscr();
printf("Enter a positive integer\n");
scanf("%d",&num);
if(num<=0)
  printf("Invalid number\n"); //to check if num is a positive integer
else
{
  printf("Reverse number = ");
  reverse(num); //call the recursive function reverse() passing num as parameter
}
getch();
}

void reverse(int a)
{
  if(a>0)
  {
  /* if a>0 then print a%10 i.e the rightmost digit of the number a
  and make recursive call to reverse() passing the remaining number
  except the rightmost digit i.e. a/10 as a parameter */

    printf("%d",a%10);
    reverse(a/10);
  }
}

 

Write a recursive function that accepts a positive integer in decimal and prints the corresponding binary equivalent.

#include<stdio.h>
#include<conio.h>

void main()
{
int num;
void dec_to_bin(int);
clrscr();
printf("Enter a positive integer\n");
scanf("%d",&num);
if(num<=0)
  printf("Invalid data\n"); //to check if num is a positive integer
else
{
  printf("Binary equivalent = ");
  dec_to_bin(num); //call to the recursive function dec_to_bin() passing num as parameter
}
getch();
}

void dec_to_bin(int a)
{
  if(a>0)
  {
  /* if number a is greater than 0 then make recursive call to dec_to_bin()
  passing a/2 as parameter and then printf a%2 since we have to print the
  remainders when the original number is successively divided by 2 in a
  reverse manner to get the binary equivalent of the number  */

    dec_to_bin(a/2);
    printf("%d",a%2);
  }
}

 

Write a recursive function to find ith term of Fibonacci series.

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
unsigned long int fibonacci(int);
clrscr();
printf("Enter number of the term which you want\n");
scanf("%d",&i);
if(i<=0)
  printf("Invalid\n"); //to check if the number of term is positive
else
  printf("%dth term of fibonacci series is : %lu\n",i,fibonacci(i)); //call to recursive function fibonacci() passing i as parameter

getch();
}

unsigned long int fibonacci(int i)
{
  if(i==1)
    return 0; //1st term of fibonacci series is 0
  else
    if(i==2)
      return 1; //2nd term of fibonacci series is 1
    else
      return fibonacci(i-1)+fibonacci(i-2); //if i is not equal to 1 or 2 then calculate the next term as sum of the last two consecutive terms
}

 

Write a recursive function that accepts two integers and calculates and returns their GCD using Euclid’s algorithm. GCD(m,n) = GCD(n,m) if m<n = m if n=0 = GCD(n,m%n) otherwise

#include<stdio.h>
#include<conio.h>

void main()
{
int n1,n2;
int gcd(int,int);
clrscr();
printf("Enter two positive integers\n");
scanf("%d %d",&n1,&n2);
if(n1<=0 || n2<=0)
  printf("Invalid number\n"); //to check if both numbers are positive integers
else
  printf("GCD of %d and %d = %d\n",n1,n2,gcd(n1,n2));  //function call to recursive function gcd() passing n1 and n2 as parameters
getch();
}

int gcd(int m,int n)
{
/*Euclid's algorithm */
  if(m<n)
    return gcd(n,m);
  else
    if(n==0)
      return m;
    else
      return gcd(n,m%n);
}

 

Write a recursive function that calculates the value of power xy where x is a floating point value and y is a non-negative integer.

#include<stdio.h>
#include<conio.h>

void main()
{
float x;
int y;
float power(float,int);
clrscr();
printf("Enter the base\n");
scanf("%f",&x);
printf("Enter the power\n");
scanf("%d",&y);
if(y<0)      //to check if power is a non-negative integer
  printf("Invalid power value\n");
else
  if(x==0)
    if(y==0)
      printf("Answer not defined\n"); //base and power both zero then answer is not defined
    else
      printf("Answer = 0\n"); //base=0 and power is positive then answer is 0
  else
    printf("Answer = %f\n",power(x,y)); //base not equal to 0 then call the recursive function power() passing float x and int y as parameters

getch();
}

float power(float x,int y)
{
  if(y>1)        //if power is greater than 1 then answer is x* recursive call to the function power() passing base 'x' and power one less i.e. 'y-1' as parameters
    return x*power(x,y-1);
  else
    return x; //when power=1 the answer of base x to the power 1 is the base itself...hence return x
}

 

Write a recursive function that accepts a positive integer and calculates and returns the sum of digits.

#include<stdio.h>
#include<conio.h>

void main()
{
int num;
int sum_of_digits(int);
clrscr();
printf("Enter a positive integer\n");
scanf("%d",&num);
if(num<=0)
  printf("Invalid number\n"); //to check if the number is positive
else
  printf("Sum of digits = %d\n",sum_of_digits(num)); //call to the function sum_of_digits() passing num as parameter
getch();
}

int sum_of_digits(int a) //recursive function
{
  if(a>9)       //if the number whose sum of digits is to be found is greater than 9 i.e. is a 2 or more digit number
    return a%10 + sum(a/10);    //then a%10 gives the units place digit and plus function call to the recursive function sum_of_digits passing the remaining number excepts the units place digit i.e a/10 as parameter
  else
    return a;  //if the number a is not greater than 9 then it is a single digit hence return the same number
}

 

Write a program with a recursive function that finds the factorial of a number.

#include<stdio.h>
#include<conio.h>

void main()
{
int n;
unsigned long int factorial(int);
clrscr();
printf("Enter the number whose factorial is to be found\n");
scanf("%d",&n);
if(n<0)
  printf("Invalid number\n"); //to check if number is valid to find factorial

else
  printf("Factorial of %d=%lu\n",n,factorial(n)); //function call to factorial passing integer n as a parameter

getch();
}

unsigned long int factorial(int a)
{
  if(a>0)
    return a*factorial(a-1); //if number a whose factorial is to be calculated is greater than 0 then make recursive call to factorial with parameter a-1
  else
    return 1;  //if number a is equal to 0 then return 1 since 0!=1
}

 

Write a program that prints all three digit Armstrong numbers using a function to determine whether a particular three digit number is an Armstrong number or not.

#include<stdio.h>
#include<conio.h>

void main()
{
int i,n;
int is_armstrong(int);
clrscr();
printf("All three digit armstrong numbers are\n");
for(i=100;i<=999;i++)
{
  if(is_armstrong(i)==1) //call function is_armstrong(i) for every i
    printf("%d\n",i);	 //if the returned value is 1 then the number i is an armstrong number hence print it

}
getch();
}

int is_armstrong(int num)
{
  int a,b,c,d;
  a=num/100; //hundreds place digit
  d=num%100; //remaining number
  b=d/10;    //tens place digit
  c=d%10;    //units place digit
  if(a*a*a+b*b*b+c*c*c==num) //condition to check if a number is an armstrong number
    return 1;    // if num is an armstrong number then this function returns 1
  else
    return 0; //else if num is not an armstrong number this function returns 0
}