Skip to main content

Palindromic number program

A palindromic number is a number that remains the same when its digits are reversed. In other words, it reads the same from left to right as it does from right to left. For example, 121, 22, 34543, and 12321 are all palindromic numbers. Palindromic numbers can be found in various number systems, such as binary, decimal, and hexadecimal.

Palindromic number detection in java

/*
  Java program for
  Check number is palindrome or not
*/
public class Numbers
{
    // Reverse The digits of number
    public int reverse(int num)
    {
        int result = 0;
        while (num != 0)
        {
            result = (result * 10) + (num % 10);
            // Remove last digit
            num = num / 10;
        }
        return result;
    }
    // Check whether given number is palindrome or not
    public void isPalindrome(int num)
    {
        // Compare number and its reverse form
        if (num == reverse(num))
        {
            // When both are equal
            System.out.println(num + " is palindrome");
        }
        else
        {
            // When not equal
            System.out.println(num + " is not palindrome");
        }
    }
    public static void main(String[] args)
    {
        Numbers task = new Numbers();
        // Test Case
        task.isPalindrome(121121);
        task.isPalindrome(1221);
        task.isPalindrome(1121);
        task.isPalindrome(121);
        task.isPalindrome(9100109);
        task.isPalindrome(-101);
    }
}
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in c++

// Include header file
#include <iostream>

//  Stdc++11 program for
//  Check number is palindrome or not
class Numbers
{
    // Reverse The digits of number
    public:
    int reverse(int num)
    {
        int result = 0;
        while (num != 0)
        {
            result = (result * 10) + (num % 10);
            // Remove last digit
            num = num / 10;
        }
        return result;
    }
    // Check whether given number is palindrome or not
    void isPalindrome(int num)
    {
        // Compare number and its reverse form
        if (num == reverse(num))
        {
            // When both are equal
            std::cout << num 
                      << " is palindrome" 
                      << std::endl;
        }
        else
        {
            // When not equal
            std::cout << num 
                      << " is not palindrome" 
                      << std::endl;
        }
    }
};
int main(int argc, char **argv){
    Numbers task ;
    // Test Case
    task.isPalindrome(121121);
    task.isPalindrome(1221);
    task.isPalindrome(1121);
    task.isPalindrome(121);
    task.isPalindrome(9100109);
    task.isPalindrome(-101);
    return 0;
};
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in c

// Include header file
#include <stdio.h>
// C program for
// Check number is palindrome or not

// Reverse The digits of number
int reverse(int num) 
{
    int result = 0;
    while (num != 0) 
    {
        result = (result * 10) + (num % 10);
        // Remove last digit
        num = num / 10;
    }
    return result;
}
// Check whether given number is palindrome or not
void isPalindrome(int num) 
{
    // Compare number and its reverse form
    if (num == reverse(num)) 
    {
        // When both are equal
        printf("%d is palindrome\n", num);
    } 
    else 
    {
        // When not equal
        printf("%d is not palindrome\n", num);
    }
}
int main() 
{
    // Test Case
    isPalindrome(121121);
    isPalindrome(1221);
    isPalindrome(1121);
    isPalindrome(121);
    isPalindrome(9100109);
    isPalindrome(-101);
    return 0;
}
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in golang

package main
import "fmt"
//  Golang program for
//  Check number is palindrome or not

// Reverse The digits of number
func reverse(num int) int {
    var result int = 0;
    for (num != 0) {
        result = (result * 10) + (num % 10);
        // Remove last digit
        num = num / 10;
    }
    return result;
}
// Check whether given number is palindrome or not
func isPalindrome(num int) {
    // Compare number and its reverse form
    if (num == reverse(num)) {
        // When both are equal
        fmt.Printf("%d is palindrome\n",num);
    } else {
        // When not equal
        fmt.Printf("%d is not palindrome\n",num);
    }
}
func main() {
    // Test Case
    isPalindrome(121121);
    isPalindrome(1221);
    isPalindrome(1121);
    isPalindrome(121);
    isPalindrome(9100109);
    isPalindrome(-101);
}
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in c#

// Include namespace system
using System; 
//  C# program for
//  Check number is palindrome or not
public class Numbers
{
    // Reverse The digits of number
    public int reverse(int num)
    {
        var result = 0;
        while (num != 0)
        {
            result = (result * 10) + (num % 10);
            // Remove last digit
            num = (int)(num / 10);
        }
        return result;
    }
    // Check whether given number is palindrome or not
    public void isPalindrome(int num)
    {
        // Compare number and its reverse form
        if (num == this.reverse(num))
        {
            // When both are equal
            Console.WriteLine(num + " is palindrome");
        }
        else {
            // When not equal
            Console.WriteLine(num + " is not palindrome");
        }
    }
    public static void Main(String[] args)
    {
        var task = new Numbers();
        // Test Case
        task.isPalindrome(121121);
        task.isPalindrome(1221);
        task.isPalindrome(1121);
        task.isPalindrome(121);
        task.isPalindrome(9100109);
        task.isPalindrome(-101);
    }
}
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in vb.net

' Include namespace system
Imports System 
'  Vb.net program for
'  Check number is palindrome or not
public Class Numbers
    ' Reverse The digits of number
    Public Function  reverse(ByVal num As Integer) As Integer
        Dim result As Integer = 0
        while (num <> 0)
            result = (result * 10) + (num Mod 10)
            ' Remove last digit
            num = CInt(num / 10)
        End While
        Return  result
    End Function
    ' Check whether given number is palindrome or not
    Public Sub isPalindrome(ByVal num As Integer)
        ' Compare number and its reverse form
        if (num = Me.reverse(num)) Then
            ' When both are equal
            Console.WriteLine(num.ToString() + " is palindrome")
        Else
            ' When not equal
            Console.WriteLine(num.ToString() + " is not palindrome")
        End IF
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As Numbers = New Numbers()
        ' Test Case
        task.isPalindrome(121121)
        task.isPalindrome(1221)
        task.isPalindrome(1121)
        task.isPalindrome(121)
        task.isPalindrome(9100109)
        task.isPalindrome(-101)
    End Sub
End Class
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in php

<?php 
//  Php program for
//  Check number is palindrome or not
class Numbers
{
    // Reverse The digits of number
    function reverse($num)
    {
        $result = 0;
        while ($num != 0)
        {
            $result = ($result * 10) + ($num % 10);
            // Remove last digit
            $num = (int)($num / 10);
        }
        return $result;
    }
    // Check whether given number is palindrome or not
    function isPalindrome($num)
    {
        // Compare number and its reverse form
        if ($num == $this->reverse($num))
        {
            // When both are equal
            echo $num, " is palindrome\n";
        }
        else
            {
            // When not equal
            echo $num, " is not palindrome\n";
        }
    }
}
$task = new Numbers();
// Test Case
$task->isPalindrome(121121);
$task->isPalindrome(1221);
$task->isPalindrome(1121);
$task->isPalindrome(121);
$task->isPalindrome(9100109);
$task->isPalindrome(-101);
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in node js

//  Node Js program for
//  Check number is palindrome or not
class Numbers
{
    // Reverse The digits of number
    reverse(num)
    {
        var result = 0;
        while (num != 0)
        {
            result = (result * 10) + (num % 10);
            // Remove last digit
            num = parseInt(num / 10);
        }
        return result;
    }
    // Check whether given number is palindrome or not
    isPalindrome(num)
    {
        // Compare number and its reverse form
        if (num == this.reverse(num))
        {
            // When both are equal
            console.log(num + " is palindrome");
        }
        else
        {
            // When not equal
            console.log(num + " is not palindrome");
        }
    }
}

// Start program execution
var task = new Numbers();
// Test Case
task.isPalindrome(121121);
task.isPalindrome(1221);
task.isPalindrome(1121);
task.isPalindrome(121);
task.isPalindrome(9100109);
task.isPalindrome(-101);
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in typescript

//  Typescript program for
//  Check number is palindrome or not
class Numbers
{
    // Reverse The digits of number
    public number reverse(num:number)
    {
        var result = 0;
        while (num != 0)
        {
            result = (result * 10) + (num % 10);
            // Remove last digit
            num = parseInt(num / 10);
        }
        return result;
    }
    // Check whether given number is palindrome or not
    public  isPalindrome(num:number)
    {
        // Compare number and its reverse form
        if (num == this.reverse(num))
        {
            // When both are equal
            console.log(num + " is palindrome");
        }
        else
        {
            // When not equal
            console.log(num + " is not palindrome");
        }
    }
}
var task = new Numbers();
// Test Case
task.isPalindrome(121121);
task.isPalindrome(1221);
task.isPalindrome(1121);
task.isPalindrome(121);
task.isPalindrome(9100109);
task.isPalindrome(-101);
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in python

#  Python 3 program for
#  Check number is palindrome or not
class Numbers :
    # Reverse The digits of number
    def  reverse(self, num) :
        result = 0
        while (num != 0) :
            result = (result * 10) + (num % 10)
            # Remove last digit
            num = int(num / 10)
        return result
    # Check whether given number is palindrome or not
    def isPalindrome(self, num) :
        # Compare number and its reverse form
        if (num == self.reverse(num)) :
            # When both are equal
            print(num,"is palindrome")
        else :
            # When not equal
            print(num, "is not palindrome")


if __name__=="__main__":
    task = Numbers()
    # Test Case
    task.isPalindrome(121121)
    task.isPalindrome(1221)
    task.isPalindrome(1121)
    task.isPalindrome(121)
    task.isPalindrome(9100109)
    task.isPalindrome(-101)
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is not palindrome

Palindromic number detection in ruby

#  Ruby program for
#  Check number is palindrome or not
class Numbers
    # Reverse The digits of number
    def reverse( num)
        result = 0
        while (num != 0)
            result = (result * 10) + (num % 10)
            # Remove last digit
            num = (num / 10).to_i
        end
        return result
    end
    # Check whether given number is palindrome or not
    def isPalindrome( num)
        # Compare number and its reverse form
        if (num == self.reverse(num))
            # When both are equal
            print(num," is palindrome\n")
        else
            # When not equal
            print(num," is not palindrome\n")
        end
    end
end

task = Numbers.new()
# Test Case
task.isPalindrome(121121)
task.isPalindrome(1221)
task.isPalindrome(1121)
task.isPalindrome(121)
task.isPalindrome(9100109)
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome

Palindromic number detection in scala

//  Scala program for
//  Check number is palindrome or not
class Numbers ()
{
    // Reverse The digits of number
    def reverse(value : Int) : Int=
    {
        var num = value;
        var result = 0
        while (num != 0)
        {
            result = (result * 10) + (num % 10)
            // Remove last digit
            num = num / 10
        }
        return result
    }
    // Check whether given number is palindrome or not
    def isPalindrome(num : Int) : Unit=
    {
        // Compare number and its reverse form
        if (num == reverse(num))
        {
            // When both are equal
            println(num," is palindrome")
        }
        else
        {
            // When not equal
            println(num," is not palindrome")
        }
    }
}

object Main 
{
    def main(args : Array[String]) : Unit=
    {
        var task = new Numbers()
        // Test Case
        task.isPalindrome(121121)
        task.isPalindrome(1221)
        task.isPalindrome(1121)
        task.isPalindrome(121)
        task.isPalindrome(9100109)
        task.isPalindrome(-101)
    }
}
Output
(121121, is palindrome)
(1221, is palindrome)
(1121, is not palindrome)
(121, is palindrome)
(9100109, is not palindrome)
(-101, is palindrome)

Palindromic number detection in swift

import Foundation
//  Swift program for
//  Check number is palindrome or not
class Numbers
{
    // Reverse The digits of number
    func reverse(_ value : Int) -> Int
    {
        var num = value;
        var result : Int = 0;
        while (num != 0)
        {
            result = (result * 10) + (num % 10);
            // Remove last digit
            num = num / 10;
        }
        return result;
    }
    // Check whether given number is palindrome or not
    func isPalindrome(_ num : Int)
    {
        // Compare number and its reverse form
        if (num == self.reverse(num))
        {
            // When both are equal
            print(String(num) + " is palindrome");
        }
        else
        {
            // When not equal
            print(String(num) + " is not palindrome");
        }
    }
}

let task : Numbers = Numbers();
// Test Case
task.isPalindrome(121121);
task.isPalindrome(1221);
task.isPalindrome(1121);
task.isPalindrome(121);
task.isPalindrome(9100109);
task.isPalindrome(-101);
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome

Palindromic number detection in kotlin

//  Kotlin program for
//  Check number is palindrome or not
class Numbers {
    // Reverse The digits of number
    fun reverse(value : Int) : Int
    {
        var num = value;
        var result : Int = 0;
        while (num != 0)
        {
            result = (result * 10) + (num % 10);
            // Remove last digit
            num = num / 10;
        }
        return result;
    }
    // Check whether given number is palindrome or not
    fun isPalindrome(num : Int) : Unit
    {
        // Compare number and its reverse form
        if (num == this.reverse(num))
        {
            // When both are equal
            println(num.toString() + " is palindrome");
        }else
        {
            // When not equal
            println(num.toString() + " is not palindrome");
        }
    }
}
fun main(args : Array<String>) : Unit
    {
        val task : Numbers = Numbers();
        // Test Case
        task.isPalindrome(121121);
        task.isPalindrome(1221);
        task.isPalindrome(1121);
        task.isPalindrome(121);
        task.isPalindrome(9100109);
        task.isPalindrome(-101);
    }
Output
121121 is palindrome
1221 is palindrome
1121 is not palindrome
121 is palindrome
9100109 is not palindrome
-101 is palindrome




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment