Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter a 5 digit integer as input and does the following tasksThe program checks whether the input number is a palindrome or not and prints the result of this palindrome check. A number is a palindrome if it reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611; whereas 12312, 55565, 45545 and 11621 are not a palindrome. Your program should print "The number is palindrome" or "The number is not palindrome" after checking the input number.The program checks whether the input number has 5 digits or not and prints the error message "Invalid number" if the input number is not a 5 digit one.I posted my code below, I think I have a good start with reversing the number but I'm not sure how to implement the portion of how to actually check if it is a palindrome.import java.util.Scanner;public class Problem2 { public static void main(String[] args) { int number;Scanner scanner = new Scanner(System.in);System.out.println("Enter a positive integer between 1 and 5 digits long that you would like to test for a palindrome: ");num = in.nextInt();for( ;num != 0; ){reversenum = reversenum * 10;reversenum = reversenum + num%10;num = num/10;} } }

Respuesta :

Answer:

I am writing a JAVA program.  

import java.util.Scanner;//to take input from user

//class to check whether input is palindrome or not

public class CheckPalindrome{

//method to count the number of digits in the input number

   static void Count(int num) {    

//convert the input integer number to String using toString() method

   String number = Integer.toString(num);

/*if the length of the input number which was converted to string is greater than 5 this means that number of digits in the input number is not between 1 to 5 so Invalid number message is displayed */

   if(number.length()>5) {System.out.println("Invalid number");}

   else //when if condition evaluates to false then else part is executed

/*if number of digits in input number is between 1 to 5, calls Palindrome method to check if the input number is a palindrome */

   Palindrome(num);}

//method to check palindrome which takes input number as parameter

static void Palindrome(int num) {

   int reversenum = 0, remainder;

 int temp = num;     //temporary variable to hold input value

    while(num>0)    {  //loop checks if the input number is greater than 0

//computes the reverse of the input number

       remainder = num % 10;  

       reversenum = (reversenum*10)+remainder;    

       num = num/10;           }    

/* if the reverse of the input number is equal to the input number then its a palindrome otherwise not */

     if(temp==reversenum)  

       System.out.println("The number is palindrome");    

     else    

       System.out.println("The number is not a palindrome"); }

public static void main(String[] args) { //start of the main() function body

       Scanner scanner = new Scanner(System.in);

// creates an object of Scanner type to scan the input

//Prompts the user to enter a positive integer between 1 and 5

    System.out.print("Enter a positive integer between 1 and 5 digits long that you would like to test for a palindrome:");

    int num = scanner.nextInt(); //scans and reads the input from user

    Count(num);   }       }

//calls Count method to check if the input number has digits between 1 to 5

Explanation:

The program is well explained in the comments added to each line of the program.

Lets take an example to understand the Palindrome method logic.

Suppose the user enter 55 as input. So value of num = 55.

First the Count() method is called which first converts this input integer number to String and then checks the length of the String. If the length exceeds 5 then the Invalid Input is displayed on output screen otherwise the Palindrome method is called. Here the number of digits is 2 so Palindrome method is called.

Palindrome method takes that input number num=55 as parameter. It then declares temp variable to store this input number 55. This temp variable is used to store the original value of num before performing operations on num.

while loop again checks if num>0. num=55 so the condition evaluates to true and the body of while loop executes.

Next the statement remainder = num % 10 take modulus of num with 10 to get the right most digit of the number to compute the reverse of num. This statement gives the following result:

remainder = 55 % 10 = 5 So value of remainder = 5

Next the statement reversenum = (reversenum*10)+remainder given the following result:

                 reversenum = (0*10) + 5 = 5.

So the value of reversenum was initialized to 0 and after the above operation its value becomes reversenum =5

Next num = num/10; divides input number with 10 and computes left most digit of the number and gives the following result:

                   num = 55/10 = 5

Now the value of num = 5

Now the while loop again checks if num>0. Its again true because num=5

Now the above three statements in the while loop body give the following results:

       remainder = num % 10 = 5%10 = 5

                          remainder = 5  

       reversenum = (reversenum*10)+remainder;    

                             = 5 * 10 + 5 = 50 + 5 = 55

                        reversenum = 55

       num = num/10 = 5/10 = 0

                              num = 0

Now the while loop again checks the condition num>0 which evaluates to false as num=0. So the loop breaks.

Next the IF condition if(temp==reversenum)  is checked. From above operations we can see that temp = num which means temp = 55 as it was holding original value of input number and reversenum= 55 after the last operation of this statement  reversenum = (reversenum*10)+remainder. So the values of both temp and reversum is equal so this means that the input number num is a palindrome. So output is:

The number is palindrome

Ver imagen mahamnasir