Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.math.BigInteger;;
- public class RevSqrNmbr {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- boolean bool = true;
- while(bool) {
- System.out.println("This program will count the number of _-digit reverse square numbers.");
- System.out.println("Enter number of digits:");
- int dig = sc.nextInt();
- while (dig < 1) {
- System.out.println("Invalid number of digits. Please enter a valid number.");
- dig = sc.nextInt();
- }
- int count = 0;
- for(BigInteger i = BigInteger.TEN.pow(dig - 1); i.compareTo(BigInteger.TEN.pow(dig)) < 0; i.add(BigInteger.ONE)) if(revSq(i)) {
- System.out.println(i.toString());
- count++;
- }
- System.out.println("There are " + count + " " + dig + "-digit reverse square numbers");
- System.out.println("Continue? Y or N");
- String str = sc.next();
- if(str.equalsIgnoreCase("n")) bool = false;
- else if(!str.equalsIgnoreCase("y")) {
- boolean cont = true;
- while (cont) {
- System.out.println("Invalid answer. Please enter a valid answer.");
- str = sc.next();
- if(str.equalsIgnoreCase("n")) cont = bool = false;
- else if(str.equalsIgnoreCase("y")) cont = false;
- }
- }
- }
- }
- public static boolean revSq(BigInteger i) {
- return rev(i.pow(2)) == rev(i).pow(2);
- }
- public static BigInteger rev(BigInteger i) {
- return new BigInteger(new StringBuilder(String.valueOf(i)).reverse().toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement