Write a program called PalinPerfect.java that finds all palindrome perfect squar
ID: 3918310 • Letter: W
Question
Write a program called PalinPerfect.java that finds all palindrome perfect squares between two integers supplied as input (start and end points are excluded).
A palindrome number is a number that reads the same from the front and the back.
For example: 212, 44, 9009, 4567654
Hint: To calculate whether a number is a palindrome or not, you can first reverse the number (using the % operator and a loop, or a String) and then check for equality.
A perfect square is a number that is the square of an integer. For example: 1, 4, 9, 16, ...
Hint: Use Math.sqrt to find the square root of a number.
Some examples of palindromic perfect squares are: 4, 121, 676.
Sample I/O:
Enter the starting point N:
200
Enter the ending point M:
678
The palindromic perfect squares are as follows:
484
676
The code should be efficient and not get killed for large values of M and N.
Moreover if input M is 4 and N is 4 for example, there should be no output.
Explanation / Answer
import java.util.Scanner; public class PalinPerfect { public static boolean isPalindrome(int num) { String str = String.valueOf(num); String reverse = ""; for(int i = str.length()-1; i >=0 ; i--){ reverse += str.charAt(i); } return str.equals(reverse); } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter the starting point N:"); int start = in.nextInt(); System.out.println("Enter the ending point M:"); int end = in.nextInt(), temp; if(startRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.