11.3 – Recursion – programs to practice Part 1

by subbu on December 9, 2013

Recursion – Programs to practice

Specification: Print the sum of natural numbers from 1 to n

Program:

#include<stdio.h>
int sum(int);
int main()
{
 int n,s;
 printf("Enter the limit:");
 scanf("%d",&n);
 s=sum(n);
 printf("Sum of natural numbers %d",s);
 return 0;
}
int sum(int n)
{
 int sm;
 if(n==1)
   return 1;
 sm=n+sum(n-1);
 return sm;
}

Output:
Enter the limit:10
Sum of natural numbers 55

Specification: Accept an integer and print its factorial

Program:

#include<stdio.h>
long factorial(int);
int main()
{
 int n;
 long fact;
 printf("Enter an integer:");
 scanf("%d",&n);
 fact=factorial(n);
 printf("Factorial of the number %ld",fact);
 return 0;
}
long factorial(int n)
{
 int f;
 if(n==1)
  return 1;
 f=n*factorial(n-1);
 return f;
}

Output:
Enter an integer:8
Factorial of the number 40320

Specification: Accept any number and print the sum of all the digits using the function sum

Program:

#include<stdio.h>
int sum(int);
int main()
{
 int n,sm;
 printf("Enter any number:");
 scanf("%d",&n);
 sm=sum(n);
 printf("Sum of all the digits %d",sm);
 return 0;
}
int sum(int n)
{
 int s;
 if(n==0)
  return 0;
 s=n%10+sum(n/10);
 return s;
}

Output:
Enter any number:5647
Sum of all the digits 22

Specification: Accept any number and print it in reverse

Program:

#include<stdio.h>
int reverse(int);
int main()
{
 int n,rev;
 printf("Enter any number:");
 scanf("%d",&n);
 rev=reverse(n);
 printf("The reverse number %d",rev);
 return 0;
}
int reverse(int n)
{
 static int r;
 if(n==0)
  return 0;
 r=r*10+n%10;
 reverse(n/10);
 return r;
}

Output:
Enter any number:7645
The reverse number 5467

Specification: Accept any decimal number and print its equal binary number

Program:

#include<stdio.h>
long dectobin(int);
int main()
{
 int n;
 long bin;
 printf("Enter the decimal number:");
 scanf("%d",&n);
 bin=dectobin(n);
 printf("Binary equal %ld",bin);
 return 0;
}
long dectobin(int n)
{
 static long b;
 if(n==0)
   return 0;
 b=n%2+dectobin(n/2)*10;
 return b;
}

Output:
Enter the decimal number:97
Binary equal 1100001

Specification: Accept the base, exponent and print the result of expression

Program:

#include<stdio.h>
int power(int,int);
int main()
{
 int base,exp,res;
 printf("Enter the base:");
 scanf("%d",&base);
 printf("Enter the exponent:");
 scanf("%d",&exp);
 res=power(base,exp);
 printf("The result of expression %d",res);
 return 0;
}
int power(int b,int e)
{
 static int r=1;
 if(e==0)
  return 1;
 r=b*power(b,--e);
 return r;
}

Output:
Enter the base:5
Enter the exponent :3
The result of expression 125

Specification: Accept two numbers and print the product of them

Program:

#include<stdio.h>
int product(int,int);
int main()
{
 int a,b,pro;
 printf("Enter two numbers:\n");
 scanf("%d%d",&a,&b);
 pro=product(a,b);
 printf("Product of two numbers %d",pro);
 return 0;
}
int product(int a,int b)
{
 static int p;
 if(b==0)
  return 0;
 p=a+product(a,--b);
 return p;
}

Output:
Enter two numbers:
5
6
Product of two numbers 30

Specification: Pint Fibonacci series up to n terms

#include<stdio.h>
void fibonacci(int,int,int);
int main()
{
 int n;
 printf("How many terms to print?");
 scanf("%d",&n);
 fibonacci(0,1,n); /* initiating starting terms*/
 return 0;
}
void fibonacci(int a,int b,int n)
{
 if(n==0)
  return;
 printf("%5d",a);
 fibonacci(b,a+b,--n);
}

Execution:
How many terms to print?7
0    1    1    2    3    5    8

Nothing special in these programs because, all these programs can be solved more easily using iteration statements
We will see some programs in the next sessions which will be so easy to solve using recursion rather iteratively

Previous post:

Next post: