1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
n200080 [17]
3 years ago
6

Implement a Java program using simple console input & output and logical control structures such that the program prompts th

e 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;
Computers and Technology
1 answer:
lisabon 2012 [21]3 years ago
3 0

Answer:

import java.util.Scanner;

import java.util.regex.*;

public class Problem2 {

   public static void main(String[] args)

   {

       int number;

       Scanner scanner = new Scanner(System.in);

       System.out.println("Enter a positive integer with exactly 5 digits long that you would like to test for a palindrome: ");

       String str = scanner.nextLine();

       int len = str.length();

       

       if (!Pattern.matches("^[1-9]\\d{4}$", str)) {

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

       } else {

           boolean isPalindrome = true;

           for(int i=0; i<len/2; i++) {

               if (str.charAt(i) != str.charAt(len-i-1)) {

                   isPalindrome = false;

                   break;

               }

           }

           if (isPalindrome) {

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

           } else {

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

           }

       }        

   }

}

Explanation:

Even though the problem talks about numbers, a palindrome check is more about strings, so that's how I'd handle it. I check the validity of the input using a regular expression (note how I don't allow the number to start with a zero, you can change that if you want), and then I check for palindrome to iterating to only halfway the string, and checking the i-th position ffrom the start against the i-th position from the end. If there is no match, the loop is aborted.

See that also for even-length palindromes this will work.

You might be interested in
Many common web page designs use columns that are about ____ of the width of the page to approximate traditional aesthetic propo
Rom4ik [11]
<span>Many common web page designs use columns that are about a third of the width of the page to approximate traditional aesthetic proportions.</span>
8 0
4 years ago
What do @ stand for?
Nikitich [7]
@ is commonly known as an "at" sign, otherwise the official name is Commercial at.
8 0
4 years ago
Please help me with coding!
tresset_1 [31]

Answer:

key code space

Explanation:

the transformation

6 0
3 years ago
Read 2 more answers
A user contacted you to report that an unwanted Windows application is launched each time her computer is booted. How can you pe
nikklg [1K]

Answer:

Uninstall the application.

Explanation:

When a program or a computer application constantly launches by itself each time your system is booted, the best and most effective solution is to uninstall the application.

Uninstalling an application entails removing the application entirely from your device.

3 0
4 years ago
To start slide show of a presentation from the first slide.
ipn [44]
The answer is a




step by step explanation:
6 0
3 years ago
Read 2 more answers
Other questions:
  • Write a program that reads in 10 numbers from the user and stores them in a 1D array of size 10. Then, write BubbleSort to sort
    13·1 answer
  • A typical, small, dry cell battery has a voltage of A. 6.0 volts. B. 1.0 volts. C. 12.0 volts. D. 1.5 volts.
    11·2 answers
  • When several computers connected to the Internet have been set up to forward spam and sometimes viruses to other computers on th
    9·1 answer
  • Professional photography is a competitive job field. <br> true <br> false
    12·2 answers
  • The set of folders and subfolders that MATLAB searches through to locate a command or M-file is called the ___________.(fill in
    9·1 answer
  • A computer system has a 32KB, 8-way set associative cache, and the block size is 8 bytes. The machine is byte addressable, and p
    7·1 answer
  • If my computer is next to my book, what preposition would I use?<br> O xià<br> 0 shàng<br> pangbian
    7·1 answer
  • How do you begin typing a table cell
    8·1 answer
  • Question 1 of 10 Chase lives in Oregon but works for a company that is located in Florida. What business trend is this an exampl
    11·2 answers
  • Can you move it like this? I can shake it like that
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!