Computer Science, asked by TheKnowledge, 11 months ago

write a Java Program to check whether a number is palindrome or not ?? ​

Answers

Answered by sagarnirapure914
12

Answer:

How to check palindrome is a number or not in Java Program :

Checking whether a number is palindrome or not is a classical Java homework exercise. When you start learning Java programming most institute which teach Java programming provides these classical Java programs like How to find Armstrong number in Java or how to find Fibonacci number in Java using recursion etc. Write a Java program to check if number is palindrome comes from same category. For those who are not familiar with palindrome numbers, palindrome number is a number which is equal to reverse of itself. For example 121 is a palindrome because reverse of 121 is 121, while 123 is not a palindrome in Java because reverse of 123 is 321 and 121!=321. Finding palindrome number is easy in Java, you just need to develop logic to reverse a number in Java. Thankfully Java provides couple of arithmetic operators like remainder(%) operator also known as modules operator, which returns remainder in division operator and division operator(/) which returns quotient. By using remainder and division operator in Java we can create program to check if number is palindrome or not.

Answered by prashant247
7

t java.io.*;

import java.util.*;

class GFG

{

// A function that reurns true

// only if num contains one digit

public static int oneDigit(int num)

{

// comparison operation is

// faster than division

// operation. So using

// following instead of

// "return num / 10 == 0;"

if((num >= 0) &&(num < 10))

return 1;

else

return 0;

}

// A recursive function to

// find out whether num is

// palindrome or not.

// Initially, dupNum contains

// address of a copy of num.

public static int isPalUtil(int num,

int dupNum)

{

// Base case (needed for recursion

// termination): This statement

// mainly compares the first digit

// with the last digit

if (oneDigit(num) == 1)

if(num == (dupNum) % 10)

return 1;

else

return 0;

// This is the key line in

// this method. Note that

// all recursive calls have

// a separate copy of num,

// but they all share same

// copy of *dupNum. We divide

// num while moving up the

// recursion tree

if (isPalUtil((int)(num / 10), dupNum) == 0)

return -1;

// The following statements

// are executed when we move

// up the recursion call tree

dupNum = (int)(dupNum / 10);

// At this point, if num%10

// contains i'th digit from

// beiginning, then (*dupNum)%10

// contains i'th digit from end

if(num % 10 == (dupNum) % 10)

return 1;

else

return 0;

}

// The main function that uses

// recursive function isPalUtil()

// to find out whether num is

// palindrome or not

public static int isPal(int num)

{

// If num is negative,

// make it positive

if (num < 0)

num = (-num);

// Create a separate copy

// of num, so that modifications

// made to address dupNum

// don't change the input number.

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

return isPalUtil(num, dupNum);

}

// Driver Code

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");

}

Similar questions