C PROGRAMS : RECURSION

C PROGRAMS : RECURSION
1. Program to find the factorial of a Number using recursion
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int fact(int); // Function prototype

int main()
{
// Declaring variable n=number
int n;

// Decalring variable f = to hold the value of factorial
int f;

// Inputting Number
printf("Enter the Number : ");
scanf("%d",&n);

// Calling Factorial function
f=fact(n);

// Printing Factorial
printf("Factorial of %d = %d ",n,f);

getch(); // Linux user - Remove this

return 0;
}

int fact(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==1)
return 1;

// Continue calling function fact
else
return n*fact(n-1);
}

2. Program to print sum of n natural numbers from 1 to n ( Number ) using recursion
#include<conio.h> // Linux user - Remove this
#include<stdio.h>

// Defining function with parameter n = number
int add(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==0)
return 0;

// Continue calling function add
else
return n+add(n-1);
}
int main()
{
// Declaring variable n = number
int n;

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

// Calling function add with actual parameter "n" passed to it and displaying the value.
printf("Sum of first n numbers = %d",add(n));

getch(); // Linux user - Remove this

return 0;
}


3. Program to calculate the power using recursion. Example a^b ( Here ^ = Power sign )
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// Defining function with parameter a=Number and b=power 
int power(int a, int b)
{
 /* Base condition : Any condition where a recursive function 
 or method does not invoke itself. */
 if(b==0)
 return 1;
 
 // Continue calling function power or function invoke itself
 else
 return a*power(a,b-1);
}

int main()
{
 // Declaring variable a= Number and b=Power
 int a, b;
 
 // Inputting Number 
 printf("Enter Number : ");
 scanf("%d",&a);
 
 // Inputting Power
 printf("Enter Power : ");
 scanf("%d",&b);
 
 // Calling funciton power and displaying value returned by it.
 printf("%d ^ %d = %d ",a, b, power(a,b));
 
 getch(); // Linux user - Remove this

 return 0;
}
4. Program to find the GCD ( Greatest Common Divisior ) or HCD ( Highes Common Factor ) using recursion
/* 

LOGIC : 

The gcd of "a" and "b" is the same as the gcd of "a" and "a%b"

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// Defining function with parameter a = First Number and b=Second Number
int gcd(int a, int b)
{
 /* Base condition : Any condition where a recursive function 
 or method does not invoke itself. */
 if(b==0)
 return a;
 
 // Continue calling function gcd or function invoke itself
 else
 return gcd(b,a%b);
}

int main()
{
 // Declaring variable a=First Number and b=Second Number
 int a,b;
 
 // Inputting First Number
 printf("Enter First Number : ");
 scanf("%d",&a);
 
 // Inputting Second Number
 printf("Enter First Number : ");
 scanf("%d",&b);
 
 // Calling funciton gcd and displaying value returned by it.
 printf("GCD of %d, %d = %d",a,b,gcd(a,b));
 
 getch(); // Linux user - Remove this

 return 0;
}

5. Program to Reverse a Number using recursion
/*
LOGIC : Reverse of a number is found by r=(r*10)+d
Here r=reverser and d=digit extracted from the number.

 */

#include<conio.h> // Linux user - Remove this
#include<stdio.h>

// Declaring global variable r=reverse, d=digit
int r=0, d=0;

// Defining function with parameter n = number
int rev(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==0)
return r;

// Continue calling function rev or function invoke itself
else
{
 // Extracting digit
 d=n%10;
 
 // Finding reverse
 r=(r*10+d);
 
 // function invoke itself
 rev(n/10);
}
}

int main()
{
// Declaring variable n = number
int n;

// Inputting Number
printf("Enter the Number : ");
scanf("%d",&n);

// Calling function "rev" with actual parameter "n" passed to it and displaying the value.
printf("Reverse of number = %d",rev(n));

getch(); // Linux user - Remove this

return 0;
}

Share this

Related Posts

Previous
Next Post »