C program to check whether given number is palindrome or not using recursive function or recursion

Define palindrome number:

When we reverse digits of number from right to left, then number can be seen as similar number it was previously then it is called palindrome number.
Example: suppose number considered is 121 and after we do reversal then digits of this number still stay same, so that 121 is palindrome number

#include
#include
int palindrome(int val);
int rever=0,remain=0;
void main()
{
  int val;
  printf("n Enter a number: ");
  scanf("%d",&val);
  if(val==palindrome(val))
  {
    printf("\n %d is a palindrome no",val); 
  }
  else
  {
    printf("\n %d is not a palindrome no",val); 
  }
  getch();
} 

int palindrome(int val)
{
  if(val)
  {
    remain=val%10;
    rever=rever*10+remain;
    palindrome(val/10); 
  } 
  return rever;
}

Ouput:

Enter a number: 16461
16461 is a palindrome no

c program to check whether a number is palindrome or not using function

Palindrome number:

palindrome number can also be definded as a number which is equal to number after reversing its digits.
Example: for e.g. input is 12121, and after reversal of its digits, input number will be 12121.

Logic:

1.Input a number
2.Reverse its digits
3.match reversed number against input number

#include<stdio.h>
#include<conio.h>
int ispalindrome(int num);
int reverse(int n);
int rev=0,rem=0; 
void main()
{
  int num;
  printf("n Enter a number: ");
  scanf("%d",&num);
  if(ispalindrome(num))
  {
    printf("n %d is palindrome number",num); 
  }
  else
  {
    printf("n %d is not palindrome number,"num); 
  } 
 getch();
}

int ispalindrome(int num)
{
  if(num==reverse(num))
  {
     return 1;
  }
  else
  {
    return 0; 
  }
}

int reverse(int n)
{
  if(n)
  {
    rem=n%10;
    rev=rev*10+rem;
    reverse(n/10);
  } 
  return rev;
}

Ouput:

Enter a number: 10201
10201 is palindrome number

c program to find reverse of a number using recursion

#include<stdio.h>
#include<conio.h>
int reverse(int num); 
void main()
{
   int num, r=0;
   printf("n Enter a number: ");
   scanf("%d",&num);
   r = reverse(num);
   printf("n Reverse number = %d",r);
   getch();

} 
int rev = 0, rem = 0;  
int reverse(int num)
{
   if(num)   {
     rem=num%10;
     rev=rev*10+rem;
     reverse(num/10);
   }
   return rev;
} 

Ouput:

Enter a number: 371
Reverse number = 173

 

C program to check armstrong number using recursive function or recursion

Armstrong number is an integer which has sum of cubes of its individual digits equal to the number itself.

Example: 0,1,153,370,..

logic

  • Seperate each digit of input integer and then calculate its cube.
  • Add the cube of all digits
  • check final sum against input integer

Program

program to check armstrong number using recursive function or recursion

#include<stdio.h>
#include<conio.h>
int armstrong(int num);
void main()
{
   int num, n;
   printf("n Enter a number: ");
   scanf("%d", &num);
   n = armstrong(num);
   printf("n %d is armstrong number",n);
   getch();
}
int armstrong(int num)
{ 
    int s = 0;
    if(num)
    {
      s = ((num%10)*(num%10)*(num%10)) + armstrong(num/10);
    }
    return s;
}

Output


input = 407
output = (4*4*4) + (0*0*0) + (7*7*7)
input  number equals sum of cube of all its digits.
So 407 is armstrong number
error: Content is protected !!