Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts
C PROGRAMS : RECURSION

C PROGRAMS : RECURSION

C PROGRAMS : RECURSION
6. Program to find whether a number is Palindrome or not using recursion
/*

Palindrome Number : If Reverse of a number = Number, the number is called Palindrome No.

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;

// Declaring variable "r" to hold the reverse number
int r;

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

// Calling function "rev" with actual parameter "n" passed to it
r=rev(n);

// Checking and Displaying if a Number is palindrome or Not
if(r==n)
printf("%d is a Palindrome Number ",n);

else
printf("%d is not a Palindrome Number ",n);

getch(); // Linux user - Remove this

return 0;
}

7. Program to find whether a number is Armstrong or not using recursion
/*

Armstrong Number : If sum of digits cube = Number then it is called an Armstrong Number

LOGIC : Sum of digits cube of a number is found by s=s+d*d*d
Here s=sum 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 s=0, d=0;

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

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

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

// Declaring variable "s" to hold the sum of digits cube of number
int s;

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

// Calling function "sum" with actual parameter "n" passed to it
s=sum(n);

// Checking and Displaying if a Number is Armstron or Not
if(s==n)
printf("%d is an Armstrong Number ",n);

else
printf("%d is not an Armstrong Number ",n);

getch(); // Linux user - Remove this

return 0;
}


8. Program to print the fibonacci series upto nth terms using recursion
/* 

Fibonacci Series : 0 1 1 2 3 5 8 13 21 34 upto nth terms.

*/

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

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

else if(n==2)
return 1;

// Continue calling function fibo
else if(n>2)
return fibo(n-1)+fibo(n-2);
}

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

/* Declaring variable "i" to iterate loop and 
"term"=holds the current number to help print the fibonacci series */
int i, term;

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

// Calling function fibo with actual parameter "n" passed to it and displaying the value.
for(i=1;i<=n;i++)
{
 term=fibo(i);
 printf("%d ",term);
}

getch(); // Linux user - Remove this

return 0;
}


9. Program to print first "n" natural numbers in reverse order
#include<conio.h> // Linux user - Remove this

#include<stdio.h>

// Defining function with parameter n = number
void natural(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n<=1)
printf("%d ",n);

// Continue calling function natural
else
{
 printf("%d ",n);
 natural(n-1);
}


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

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

// Displaying the value.
printf("First %d Natural Numbers in reverse order : \n",n);

// Calling function natural with actual parameter "n" passed to it 
natural(n);

getch(); // Linux user - Remove this

return 0;
}


10. Program to print pattern :
/*

*
* *
* * * 
* * * *
* * * * *

 */

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

int recursion(i,j)
{
if(i>5)
return 0;
else if(j<=i)
{
 printf("* ");
 recursion(i,j+1);
}
else
{
printf("\n");
recursion(i+1,1); 
}
}
int main()
{ 
 recursion(1,1);
 
 getch(); // Linux user - Remove this

 return 0;
}

11. Program to print pattern :
/*

*
* *
* * * 
* * * *
* * * * * till n terms

 */


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

int recursion(i,j,n)
{
if(i>n)
return 0;
else if(j<=i)
{
 printf("* ");
 recursion(i,j+1,n);
}
else
{
printf("\n");
recursion(i+1,1,n); 
}
}
int main()
{ int n;
 printf("Enter the value till which you want to print the patter:");
 scanf("%d",&n);
 recursion(1,1,n);
 
 getch(); // Linux user - Remove this

 return 0;
}
C PROGRAMS : RECURSION

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;
}