![Welcomes](/avatars/9581.jpg)
Welcomes
14.12.2021 •
Computers and Technology
Indian girls that like seeing sex/nrg-tznm-nhv
Solved
Show answers
More tips
- P Photography and Videography What is lens calibration and why is it needed?...
- F Family and Home Stay Warm but Don t Overheat: What is the Optimal Temperature for Your Home During Winter?...
- H Health and Medicine How to Treat the Flu: A Comprehensive Guide...
- O Other What is a Disk Emulsifier and How Does it Work?...
- F Family and Home What does a newborn need?...
- F Family and Home Choosing the Right Car Seat for Your Child: Tips and Recommendations...
- F Food and Cooking How to Get Reconfirmation of Registration?...
- C Computers and Internet How to Get Rid of Spam in ICQ?...
- A Art and Culture Who Said The Less We Love a Woman, the More She Likes Us ?...
- F Family and Home How to Get Rid of Your Neighbors?...
Answers on questions: Computers and Technology
- M Mathematics At a store,customers are randomly...
- E English What is the name of that thing that is inside chicken that is contain sand?...
- A Advanced Placement (AP) Which among these is a biotic factor? a) weather b) water temperature c) interactions between organisms d) sunlight...
- E English In a short paragraph, compare and contrast to different items (i.e. pens/pencils, m& ms/skittles, etc....
Ответ:
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.