Computer Science, asked by nihal0077, 2 months ago

Write A program in java to input a number & check whether the given no. is a Pallindrome no. or not. If it is not a Pallindrome than print the nearest Pallindrome.​

Answers

Answered by anindyaadhikari13
5

Required Answer:-

Question:

  • Write a program in Java to input a number and check whether the given number is Palindrome or not. If it is not a Palindrome, then print the nearest Palindrome.

Solution:

Here is the program.

import java.util.*;

public class Palindrome {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt();

if(isPalindrome(n))

System.out.println("Number is Palindrome.");

else {

System.out.println("Number is not Palindrome.");

System.out.println("Nearest Palindrome is: "+nearestPalindrome(n));

}

sc.close();

}

static boolean isPalindrome(int n) {

int m=n, s=0;

while(m!=0) {

s=s*10+m%10;

m/=10;

}

return (s==n);

}

static int nearestPalindrome(int n) {

int higherRange=0, lowerRange=0;

for(int i=n+1;;i++) {

if(isPalindrome(i)) {

higherRange=i;

break;

}

}

for(int i=n-1;;i--) {

if(isPalindrome(i)) {

lowerRange=i;

break;

}

}

int d1=higherRange-n, d2=n-lowerRange;

if(d1<d2)

return higherRange;

return lowerRange;

}

}

Explanation:

  • Two functions are created - (i) isPalindrome(n) and (ii) nearestPalindrome(n)
  • isPalindrome(n):- This function check whether a number is Palindrome or not.
  • nearestPalindrome(n):- Remember that we need to find out the nearest palindrome. For example, nearest palindrome of 122 is 121 and not 131. Similarly, nearest palindrome of 128 is 131.
  • To find out nearest palindrome, we will find out the nearest palindrome number above the given number and the nearest palindrome below the number. Now, we will find out the difference. If the difference between the palindrome in the higher range and the number is greater than the other, then it means that the palindrome in the lower range is the nearest one or else, the palindrome in the higher range is the nearest one.
  • For example, let us say n = 122, first palindrome after 122 is 131 and the first palindrome before 122 is 121. Now, 122 - 121 = 1 where as 131 - 122 = 9. This means that 131 is far away from 122 as compared to 121. So, nearest palindrome is 121.

Output is attached (5 pics).

Attachments:

nihal0077: aap konsa application use karte hai??
nihal0077: phone par java use karne k liye???
nihal0077: and java related aur bui questions hai mere aap usko answer kar sakte hai kya???
anindyaadhikari13: You can download JAVA N-IDE from Play Store.
anindyaadhikari13: I will contact you tomorrow.
Anonymous: Perfect :)
Answered by Oreki
5

Program:

import java.util.Scanner;

public class NearestPalindrome {

   static boolean isPalindrome(int number) {

       return new StringBuilder(number + "").reverse( ).toString( ).equals(number + "");

   }

   public static void main(String[ ] args) {

       System.out.print("Enter a number - ");

       int number = new Scanner(System.in).nextInt( );

       if (isPalindrome(number)) {

           System.out.println("Palindrome number");

       } else {

          int forward, backward;

           // Checking palindromes succeeding the number.

           for (forward = number + 1; !isPalindrome(forward); ++forward) ;

           // Checking palindromes preceding the number.

           for (backward = number - 1; !isPalindrome(backward); --backward) ;

           // Finding the most closest number among the preceding and succeeding.

           System.out.print("Nearest Palindrome - ");

           if (Math.abs(number - forward) < Math.abs(number - backward)) {

               System.out.println(forward);

           } else System.out.println(backward);

       }

   }

}

Algorithm:

  • Accepting the number to be checked.
  • If not a Palindrome then, checking for Palindromes before and after the number.
  • Finding the nearest number between the two.
  • Displaying the nearest Palindrome.
Attachments:

Anonymous: Perfect :)
Oreki: Thanks!
nihal0077: nice answer pal
Oreki: Thanks!
Similar questions