C Recursion Program Example
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
/*PROGRAM-2*/
/*Write a program in C to calculate the sum of numbers from 1 to n using recursion.*/
Pictorial Presentation:
#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);
}
/*============================OUTPUT============================
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");
}
if (i == term)
return (0);
else
{
i++;
fibonacci(prNo, num); //recursion, calling the function fibonacci itself
}
return (0);
}
Sample Solution:
C Code:
/*PROGRAM-4*/
/*Write a program in C to find GCD of two numbers using recursion.*/
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;
}
/*PROGRAM-5*/
Write a program in C to find the Factorial of a number using recursion.
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
/*PROGRAM-6*/
Write a program in C to check a number is a prime number or not using recursion.
Pictorial Presentation:
/*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;
i = n1/2;
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;
}
/*
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
/*PROGRAM-7*/
Write a program in C to find the LCM of two numbers using recursion.
Pictorial Presentation:
/*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================================
*/
/* PROGRAM -8*/
Write a program in C to calculate the power of any number using recursion.
Pictorial Presentation:
/* PROGRAM -8*/
/* Write a program in C to calculate the power of any number using recursion.*/
#include <stdio.h>
return 0;
}
/*============================OUTPUT=================
*/
/*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;
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===========================
*/
/*PROGRAM-10*/
Write a program in C to print even or odd numbers in given range using recursion.
Pictorial Presentation:
/* 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");
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=============
===================================END========================