C Recursion Program Example
/* PROGRAM-1*/
Write a program in C to print first 50 natural numbers using recursion.
Pictorial Presentation:
Sample Solution:
C Code:
/* PROGRAM-1*/
/*Write a program in C to print first 50 natural numbers using recursion.*/
#include<stdio.h>
int numPrint(int);
int main()
{
int n = 1;
printf("\n\n Recursion : print first 50 natural numbers :\n");
printf("-------------------------------------------------\n");
printf(" The natural numbers are :");
numPrint(n);
printf("\n\n");
return 0;
}
int numPrint(int n)
{
if(n<=50)
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
{
printf(" %d ",n);
numPrint(n+1);
}
}
/*
PS C:\Users\Subhas> CD ESCS291PROG
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc NaturalNumber.c -o NaturalNumber
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./NaturalNumber
Recursion : print first 50 natural numbers :
-------------------------------------------------
The natural numbers are : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
*/
/*PROGRAM-2*/
/*Write a program in C to calculate the sum of numbers from 1 to n using recursion.*/
Pictorial Presentation:
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
Sample Solution:
C Code:
/*PROGRAM-2*/
/*Write a program in C to calculate the sum of numbers from 1 to n using recursion.*/
#include<stdio.h>
int sumOfRange(int);
int main()
{
int n1;
int sum;
printf("\n\n Recursion : calculate the sum of numbers from 1 to n :\n");
printf("-----------------------------------------------------------\n");
printf(" Input the last number of the range starting from 1 : ");
scanf("%d", &n1);
sum = sumOfRange(n1);
printf("\n The sum of numbers from 1 to %d : %d\n\n", n1, sum);
return (0);
}
int sumOfRange(int n1)
{
int res;
if (n1 == 1)
{
return (1);
} else
{
res = n1 + sumOfRange(n1 - 1); //calling the function sumOfRange itself
}
return (res);
}
/*============================OUTPUT============================
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc SumofNatural.c -o SumofNatural
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./SumofNatural
Recursion : calculate the sum of numbers from 1 to n :
-----------------------------------------------------------
Input the last number of the range starting from 1 : 10
The sum of numbers from 1 to 10 : 55
*/
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
/*PROGRAM-3*/
Write a program in C to print Fibonacci Series using recursion.
Pictorial Presentation:
Sample Solution:
C Code:
/*PROGRAM-3*/
/* Write a program in C to print Fibonacci Series using recursion.*/
#include<stdio.h>
int term;
int fibonacci(int prNo, int num);
void main()
{
static int prNo = 0, num = 1;
printf("\n\n Recursion : Print Fibonacci Series :\n");
printf("-----------------------------------------\n");
printf(" Input number of terms for the Series (< 20) : ");
scanf("%d", &term);
printf(" The Series are :\n");
printf(" 1 ");
fibonacci(prNo, num);
printf("\n\n");
}
int fibonacci(int prNo, int num)
{
static int i = 1;
int nxtNo;
if (i == term)
return (0);
else
{
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
nxtNo = prNo + num;
prNo = num;
num = nxtNo;
printf("%d ", nxtNo);
i++;
fibonacci(prNo, num); //recursion, calling the function fibonacci itself
}
return (0);
}
/*PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc Fibonacci.c -o Fibonacci
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./Fibonacci
Recursion : Print Fibonacci Series :
-----------------------------------------
Input number of terms for the Series (< 20) : 7
The Series are :
1 1 2 3 5 8 13
*/
/*PROGRAM-4*/
Write a program in C to find GCD of two numbers using recursion.
Pictorial Presentation:
Sample Solution:
C Code:
/*PROGRAM-4*/
/*Write a program in C to find GCD of two numbers using recursion.*/
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
#include<stdio.h>
int findGCD(int num1,int num2);
int main()
{
int num1,num2,gcd;
printf("\n\n Recursion : Find GCD of two numbers :\n");
printf("------------------------------------------\n");
printf(" Input 1st number: ");
scanf("%d",&num1);
printf(" Input 2nd number: ");
scanf("%d",&num2);
gcd = findGCD(num1,num2);
printf("\n The GCD of %d and %d is: %d\n\n",num1,num2,gcd);
return 0;
}
int findGCD(int a,int b)
{
while(a!=b)
{
if(a>b)
return findGCD(a-b,b);
else
return findGCD(a,b-a);
}
return a;
}
/*PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc GCD.c -o GCD
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./GCD
Recursion : Find GCD of two numbers :
------------------------------------------
Input 1st number: 45
Input 2nd number: 3
The GCD of 45 and 3 is: 3
*/
/*PROGRAM-5*/
Write a program in C to find the Factorial of a number using recursion.
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
Pictorial Presentation:
Sample Solution:
C Code:
/* PROGRAM-5*/
/*Write a program in C to find the Factorial of a number using recursion.*/
#include<stdio.h>
int findFactorial(int);
int main()
{
int n1,f;
printf("\n\n Recursion : Find the Factorial of a number :\n");
printf("-------------------------------------------------\n");
printf(" Input a number : ");
scanf("%d",&n1);
f=findFactorial(n1);//call the function findFactorial for factorial
printf(" The Factorial of %d is : %d\n\n",n1,f);
return 0;
}
int findFactorial(int n)
{
if(n==1)
return 1;
else
return(n*findFactorial(n-1));// calling the function findFactorial to itself recursively
}
/*
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc Factrorial.c -o Factrorial
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./Factrorial
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
Recursion : Find the Factorial of a number :
-------------------------------------------------
Input a number : 7
The Factorial of 7 is : 5040
*/
/*PROGRAM-6*/
Write a program in C to check a number is a prime number or not using recursion.
Pictorial Presentation:
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
Sample Solution:
C Code:
/*PROGRAM-6*/
/*Write a program in C to check a number is a prime number or not using recursion.*/
#include<stdio.h>
int checkForPrime(int);
int i;
int main()
{
int n1,primeNo;
printf("\n\n Recursion : Check a number is prime number or not :\n");
printf("--------------------------------------------------------\n");
printf(" Input any positive number : ");
scanf("%d",&n1);
i = n1/2;
primeNo = checkForPrime(n1);//call the function checkForPrime
if(primeNo==1)
printf(" The number %d is a prime number. \n\n",n1);
else
printf(" The number %d is not a prime number. \nn",n1);
return 0;
}
int checkForPrime(int n1)
{
if(i==1)
{
return 1;
}
else if(n1 %i==0)
{
return 0;
}
else
{
i = i -1;
checkForPrime(n1);//calling the function checkForPrime itself recursively
}
}
/*
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc PrimeNumber.c -o PrimeNumber
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./PrimeNumber
Recursion : Check a number is prime number or not :
--------------------------------------------------------
Input any positive number : 9
The number 9 is not a prime number.
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc PrimeNumber.c -o PrimeNumber
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./PrimeNumber
Recursion : Check a number is prime number or not :
--------------------------------------------------------
Input any positive number : 7
The number 7 is a prime number.
*/
/*PROGRAM-7*/
Write a program in C to find the LCM of two numbers using recursion.
Pictorial Presentation:
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
Sample Solution:
C Code:
/*PROGRAM-7*/
/* Write a program in C to find the LCM of two numbers using recursion. */
#include <stdio.h>
int lcmCalculate(int a, int b);
int main()
{
int n1, n2, lcmOf;
printf("\n\n Recursion : Find the LCM of two numbers :\n");
printf("----------------------------------------------\n");
printf(" Input 1st number for LCM : ");
scanf("%d", &n1);
printf(" Input 2nd number for LCM : ");
scanf("%d", &n2);
// Ensures that first parameter of lcm must be smaller than 2nd
if(n1 > n2)
lcmOf = lcmCalculate(n2, n1);//call the function lcmCalculate for lcm calculation
else
lcmOf = lcmCalculate(n1, n2);//call the function lcmCalculate for lcm calculation
printf(" The LCM of %d and %d : %d\n\n", n1, n2, lcmOf);
return 0;
}
int lcmCalculate(int a, int b)//the value of n1 and n2 is passing through a and b
{
static int m = 0;
//Increments m by adding max value to it
m += b;
// If found a common multiple then return the m.
if((m % a == 0) && (m % b == 0))
{
return m;
}
else
{
lcmCalculate(a, b);//calling the function lcmCalculate itself
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
}
}
/*=====================OUTPUT================================
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc LCM.c -o LCM
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./LCM
Recursion : Find the LCM of two numbers :
----------------------------------------------
Input 1st number for LCM : 45
Input 2nd number for LCM : 5
The LCM of 45 and 5 : 45
*/
/* PROGRAM -8*/
Write a program in C to calculate the power of any number using recursion.
Pictorial Presentation:
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
Sample Solution:
C Code:
/* PROGRAM -8*/
/* Write a program in C to calculate the power of any number using recursion.*/
#include <stdio.h>
long int CalcuOfPower(int x,int y)
{
long int result=1;
if(y == 0) return result;
result=x*(CalcuOfPower(x,y-1)); //calling the function CalcuOfPower itself recursively
}
int main()
{
int bNum,pwr;
long int result;
printf("\n\n Recursion : Calculate the power of any number :\n");
printf("----------------------------------------------------\n");
printf(" Input the base value : ");
scanf("%d",&bNum);
printf(" Input the value of power : ");
scanf("%d",&pwr);
result=CalcuOfPower(bNum,pwr);//called the function CalcuOfPower
printf(" The value of %d to the power of %d is : %ld\n\n",bNum,pwr,result);
return 0;
}
/*============================OUTPUT=================
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc Power.c -o Power
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./Power
Recursion : Calculate the power of any number :
----------------------------------------------------
Input the base value : 3
Input the value of power : 4
The value of 3 to the power of 4 is : 81
*/
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
/*PROGRAM-9*/
Write a program in C for binary search using recursion.
Pictorial Presentation:
/*PROGRAM-9*/
/*Write a program in C for binary search using recursion.*/
#include <stdio.h>
int binarySearch(int*, int, int, int, int);
int main()
{
int arr1[10], i, n, md, c, low, hg;
printf("\n\n Recursion : Binary searching :\n");
printf("-----------------------------------\n");
printf(" Input the number of elements to store in the array :");
scanf("%d", &n);
printf(" Input %d numbers of elements in the array in ascending order :\n", n);
for (i = 0; i < n; i++)
{
printf(" element - %d : ", i);
scanf("%d", &arr1[i]);
}
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
printf(" Input the number to search : ");
scanf("%d", &md);
low = 0, hg = n - 1;
c = binarySearch(arr1, n, md, low, hg);
if (c == 0)
printf(" The search number not exists in the array.\n\n");
else
printf(" The search number found in the array.\n\n");
return 0;
}
int binarySearch(int arr1[], int n, int md, int low, int hg)
{
int mid, c = 0;
if (low <= hg)
{
mid = (low + hg) / 2;
if (md == arr1[mid])
{
c = 1;
}
else if (md < arr1[mid])
{
return binarySearch(arr1, n, md, low, mid - 1);
}
else
return binarySearch(arr1, n, md, mid + 1, hg);
}
else
return c;
}
/*========================OUTPUT===========================
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc BinarySearch.c -o BinarySearch
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./BinarySearch
Recursion : Binary searching :
-----------------------------------
Input the number of elements to store in the array :3
Input 3 numbers of elements in the array in ascending order :
element - 0 : 4
element - 1 : 6
element - 2 : 9
Input the number to search : 6
The search number found in the array.
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc BinarySearch.c -o BinarySearch
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./BinarySearch
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
Recursion : Binary searching :
-----------------------------------
Input the number of elements to store in the array :4
Input 4 numbers of elements in the array in ascending order :
element - 0 : 7
element - 1 : 9
element - 2 : 3
element - 3 : 5
Input the number to search : 2
The search number not exists in the array.
*/
/*PROGRAM-10*/
Write a program in C to print even or odd numbers in given range using recursion.
Pictorial Presentation:
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
Sample Solution:
C Code:
/*PROGRAM-10*/
/* Write a program in C to print even or odd numbers in given range using recursion.*/
#include <stdio.h>
void EvenAndOdd(int stVal, int n);
int main()
{
int n;
printf("\n\n Recursion : Print even or odd numbers in a given range :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the range to print starting from 1 : ");
scanf("%d", &n);
printf("\n All even numbers from 1 to %d are : ", n);
EvenAndOdd(2, n);//call the function EvenAndOdd for even numbers
printf("\n\n All odd numbers from 1 to %d are : ", n);
EvenAndOdd(1, n);// call the function EvenAndOdd for odd numbers
printf("\n\n");
return 0;
}
void EvenAndOdd(int stVal, int n)
{
if(stVal > n)
return;
printf("%d ", stVal);
EvenAndOdd(stVal+2, n);//calling the function EvenAndOdd itself recursively
}
/*
==============================OUTPUT=============
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> gcc EvenOdd.c -o EvenOdd
PS C:\Users\Subhas\Desktop\ESCS291PROG\RECURSION> ./EvenOdd
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT
Recursion : Print even or odd numbers in a given range :
-------------------------------------------------------------
Input the range to print starting from 1 : 15
All even numbers from 1 to 15 are : 2 4 6 8 10 12 14
All odd numbers from 1 to 15 are : 1 3 5 7 9 11 13 15
*/
===================================END========================
ESCS291 RECURSION SUBHAS HALDER@CSE ||GMIT