Check whether a number is PALINDROME or Not | faceprep

Program to check whether the given number is a palindrome or not is discussed here. Any number is said to be a palindrome if the original number and the reverse of the original number are the same.

For example, 1234321 is a palindrome.

  • Original number = 1234321
  • The reverse of the number = 1234321

palindrome of a a number

Algorithm to check whether a number is a palindrome or not

  • Input the number. 
  • Find the reverse of the number.
  • If the reverse of the number is equal to the number, then return true. Else, return false.

A number can be checked if it is a palindrome or not in both iterative and recursive approaches. Both approaches are discussed below.

Palindrome or not using iterative approach

// C program to check if the given number is a palindrome or not

#include <stdio.h>
int main()
{
int n, reverse = 0, remainder, number;

printf(“Enter an integer: “);
scanf(“%d”, &n);

number = n;

while( n!=0 )
{
remainder = n%10;
reverse = reverse*10 + remainder;
n /= 10;
}

if (number == reverse)
printf(“\n%d is a palindrome\n”, number);
else
printf(“\n%d is not a palindrome\n”, number);

return 0;
}

// C++ program to check if the given number is a palindrome or not

#include <iostream>
using namespace std;
int main()
{
int n, reverse = 0, remainder, number;

cout << “Enter an integer: “;
cin >> n;

number = n;

while( n!=0 )
{
remainder = n%10;
reverse = reverse*10 + remainder;
n /= 10;
}

if (number == reverse)
cout << number << ” is a palindrome\n”;
else
cout << number << ” is not a palindrome\n”;

return 0;
}

// Java program to check if the given number is a palindrome or not

import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, reverse = 0, remainder, number;
System.out.print(“Enter a number : “);
n = sc.nextInt();
number = n;
while( n!=0 )
{
remainder = n%10;
reverse = reverse*10 + remainder;
n /= 10;
}
if(number == reverse)
{
System.out.println(number + ” is a palindrome\n”);
}
else
{
System.out.println(number + ” is not a palindrome\n”);
}

}
}

# Python program to check if the given number is a palindrome or not

n = int(input(“Enter the binary number : “))
reverse = 0
number = n
while( n!=0 ):
remainder = n%10
reverse = reverse*10 + remainder
n =int(n/10)
if(number == reverse):
print(number, “is a palindrome\n”)
else:
print(number, “is not a palindrome\n”)

#include<stdio.h>
#include<math.h>
int main(int a,int *b[])
{
int  number, rem, sum = 0;
number = atoi(b[1]);
int copy = number;
while(number != 0)
{
rem =number%10;
sum = sum * 10 + rem;
number = number/10;
}
if(copy == sum)
printf(“Palindrome”);
else
printf(“Not Palindrome”);
return 0;
}

Time complexity: O(n)

Palindrome or not using recursive approach

#include<stdio.h>

int is_Palindrome(int );

int n;

int main()
{
int palindrome;
printf(“\n\nEnter a number : “);
scanf(“%d”, &n);
palindrome = is_Palindrome(n);
if(palindrome == 1)
printf(“\n%d is palindrome\n”, n);
else
printf(“\n%d is not palindrome\n”, n);

return 0;
}

int is_Palindrome(int aj)
{
static int sum = 0;
if(aj != 0)
{
sum = sum *10 + aj%10;
is_Palindrome(aj/10); // recursive call
}
else if(sum == n)
return 1;
else
return 0;
}

#include<iostream>
using namespace std;

int is_Palindrome(int );

int n;

int main()
{
int palindrome;
cout << “\n\nEnter a number : “;
cin >> n;
cout << endl;
palindrome = is_Palindrome(n);
if(palindrome == 1)
cout << n << ” is a palindrome\n”;
else
cout << n << ” is not a palindrome\n”;
return 0;
}

int is_Palindrome(int aj)
{
static int sum = 0;
if(aj != 0)
{
sum = sum *10 + aj%10;
is_Palindrome(aj/10); // recursive call
}
else if(sum == n)
return 1;
else
return 0;
}

import java.util.*;

class Main
{
static int rev(int n, int temp)
{
// base case
if (n == 0)
return temp;

// stores the reverse of the number
temp = (temp * 10) + (n % 10);

return rev(n / 10, temp);
}

public static void main (String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter a number : “);
n = sc.nextInt();
int temp = rev(n, 0);

if (temp == n)
System.out.println(“yes”);
else
System.out.println(“no” );
}
}

// This code is contributed by anuj_67.

def rev(n, temp):
# base case
if (n == 0):
return temp;
# stores the reverse of a number
temp = (temp * 10) + (n % 10);
return rev(n / 10, temp);

n = 454;
temp = rev(n, 0);
if (temp != n):
print(“yes”);
else:
print(“no”);

Output:

Time complexity: O(n)

palindrome or not