Saturday, 11 August 2018

Java Program to Reverse a Number and Check in the event that it is a Palindrome


Java Program to Reverse a Number and Check in the event that it is a Palindrome

This is a Java Program to Reverse a Number and Check in the event that it is a Palindrome. A palindrome is a word, expression, number, or other arrangement of characters which peruses the same in reverse or forward.

Enter any number as an information. After that we perform different tasks like modulus and division to invert the number and check whether the given whole number is palindrome or not. Subsequently we create the yield in like manner.

Here is the source code of the Java Program to Reverse a Number and Check in the event that it is a Palindrome. The Java program is effectively assembled and kept running on a Windows framework. The program yield is additionally demonstrated as follows.

import java.util.Scanner;
public class Palindrome
{
    public static void main(String args[])
    {
        int n, m, a = 0,x;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter any number:");
        n = s.nextInt();
        m = n;
        while(n > 0)
        {
            x = n % 10;
            a = a * 10 + x;
            n = n / 10;
        }
        if(a == m)
        {
            System.out.println("Given number "+m+" is Palindrome");
        }
        else
        {
            System.out.println("Given number "+m+" is Not Palindrome");
        }
    }
}


A palindromic number or numeral palindrome is a number that remaining parts a similar when its digits are turned around. Like 16461, for instance, it is "symmetrical". The term palindromic is gotten from palindrome, which alludes to a word, (for example, rotor or racecar) whose spelling is unaltered when its letters are turned around. The initial 30 palindromic numbers (in decimal) are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202,

Check if a number is Palindrome

Given a whole number, compose a capacity that profits genuine if the given number is palindrome, else false. For instance, 12321 is palindrome, yet 1451 isn't palindrome.

Give the allowed number to be num. A basic strategy for this issue is to first turn around digits of num, at that point contrast the switch of num and num. On the off chance that both are same, at that point return genuine, else false.

Following is a fascinating technique motivated from method#2 of this post. The thought is to make a duplicate of num and recursively pass the duplicate by reference, and pass num by esteem. In the recursive calls, isolate num by 10 while moving down the recursion tree. While climbing the recursion tree, partition the duplicate by 10. When they meet in a capacity for which all tyke brings are finished, the last digit of num will be ith digit from the earliest starting point and the last digit of duplicate will be ith digit from the end.


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

class GFG
{
     

public static int oneDigit(int num)
{
    
    if((num >= 0) &&(num < 10))
    return 1;
    else
    return 0;
}


public static int isPalUtil(int num, 
                            int dupNum)
{
    
    if (oneDigit(num) == 1)
        if(num == (dupNum) % 10)
        return 1;
        else
        return 0;

   
    if (isPalUtil((int)(num / 10), dupNum) == 0)
        return -1;

    
    dupNum = (int)(dupNum / 10);

    
    if(num % 10 == (dupNum) % 10) 
        return 1; 
    else
        return 0;
}


public static int isPal(int num)
{
    
    if (num < 0)
    num = (-num);

    
    int dupNum = (num); // *dupNum = num

    return isPalUtil(num, dupNum);
}


public static void main(String args[])
{
int n = 12321;
if(isPal(n) == 0)
    System.out.println("Yes");
else
    System.out.println("No");

n = 12;
if(isPal(n) == 0)
    System.out.println("Yes");
else
    System.out.println( "No");

n = 88;
if(isPal(n) == 1)
    System.out.println("Yes");
else
    System.out.println( "No");

n = 8999;
if(isPal(n) == 0)
    System.out.println("Yes");
else
    System.out.println( "No");
}
}


0 comments:

Post a Comment