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

Share this

Related Posts

Previous
Next Post »