Program to check whether a number is a Palindrome in 6 different languages.

Program to check whether a number is a Palindrome in 6 different languages.

We come across symmetry on a daily basis in our lives in many different ways, in both the natural and man-made world. Symmetry possesses the magic power of drawing our attention. Most of us value and enjoy experiencing symmetry both visually and mentally because it conveys a sense of aesthetic balance, beauty, equality, and evenness.

A palindrome is a symmetric word, phrase, number, or another sequence of units that reads the same forward and backward.

Some interesting examples of sentences that are palindromic are - "Never odd or even", "No, it is opposition", "No lemons, no melon", "Rats live on no evil star", and "Rise to vote, Sir".

In computer programming, the program to find whether a string or number is palindrome or not is very common. It is one of the first problems taught to new learners as it explains the concept of conditional statements like if-else and for and while loops to the beginners.

In this post, I have written programs in 6 famous programming languages to solve the problem of checking whether a number is a palindrome or not.

1 - C

In C language the code for finding whether a number is a palindrome or not is given below.

#include <stdio.h>

int main(void) {
    int n, reverse = 0, rem, original;
    printf("Enter a number: ");
    scanf("%d", &n);
    original = n;

    // reversing a number and saving it in variable reverse
    // find the remainder with 10 and add it to reverse and each step multiply by 10
    // divide the number by 10 until it is 0
    while (n != 0) {
        rem = n % 10;
        reverse = reverse * 10 + rem;
        n /= 10;
    }

    // checking condition for palindrome i.e. if orignal and reverse are equal
    if (original == reverse)
        printf("%d is a palindrome.", original);
    else
        printf("%d is not a palindrome.", original);

    return 0;
}

2 - C++

In C++ the code is similar to C with difference only in taking the input and printing output.

#include <iostream>
using namespace std;

int main() {
    int n, reverse = 0, rem, original;
    cout<<"Enter a number: ";
    cin>>n;
    original = n;

    // reversing a number and saving it in variable reverse
    // find the remainder with 10 and add it to reverse and each step multiply by 10
    // divide the number by 10 until it is 0
    while (n != 0) {
        rem = n % 10;
        reverse = reverse * 10 + rem;
        n /= 10;
    }

    // checking condition for palindrome i.e. if orignal and reverse are equal
    if (original == reverse)
        cout<<original<<" is a palindrome."<<endl;
    else
       cout<<original<<" is not a palindrome."<<endl;

    return 0;
}

3 - Java

The code in Java is given below the logic is the same as mentioned above in the case of C and C++.

import java.io.*;
import java.util.Scanner; 

class Palindrome{  
 public static void main(String args[]){  
  int rem,reverse=0,original;    
  int n;

  System.out.println("Enter the number: ");

  Scanner s = new Scanner(System.in); 
  n = s.nextInt();

  original=n;    
  while(n>0){    
   rem=n%10;    
   reverse=(reverse*10)+rem;    
   n=n/10;    
  }    
  if(reverse==original)    
   System.out.println(original+ " is a palindrome number ");    
  else    
   System.out.println(original+ " is not a palindrome number");    
}  
}

4 - Python

The code in Python is simpler as there are less overheads for import and input-output operations.

print("Enter a number")
n= int(input())

original = n
rem,reverse=0,0


while(n!=0):
    rem=n%10    
    reverse=(reverse*10)+rem    
    n=n//10

if reverse == original:
    print(original ,"is a palindrome number")
else:
    print(original ,"is not a palindrome number")

If we treat the input as a string and use a string splicing method of python the code becomes much more smaller.

print("Enter a number")
n= input()

reverse= n[::-1]

if reverse == n:
    print(n ,"is a palindrome number")
else:
    print(n ,"is not a palindrome number")

5 - Perl

A similar program in Perl would look like this

print"Enter a number: ";
$n=<STDIN>;

$original=$n;
$reverse=0;

while($n>0)
{
$rem=$n%10;
$reverse=($reverse*10)+$rem;
$n=int($n/10);
}


if($original==$reverse)
{
print "$original is palindrome\n";
}
else
{
print "$original is not palindrome\n";
}

6 - Ruby

The program in Ruby to check whether a given number is palindrome or not is given below.

puts "Enter the number"
num=gets.chomp.to_i

original=num
reverse = 0

while num!=0 
    rem=num%10
    num=num/10
    reverse=reverse*10+rem
end

if(original==reverse)
    puts "#{original} is a palindrome"
else
    puts "#{original} is not a palindrome"
end

A code in which the number is converted to string and then reversed and then converted back to integer and this reversed number is compared with original is given below

puts "Enter the number"
num=gets.chomp.to_i

if(num.to_s.reverse.to_i==num)
    puts "#{num} is a palindrome"
else
    puts "#{num} is not a palindrome"
end

Hope the beginners got an idea about how to use conditional statements and loops in various languages and to check palindrome numbers using this article and it was beneficial for them. I have written the program in the languages I was comfortable with you can add the program in your favourite languages in the comments below.