Advertisement
Guest User

Reverse Square Number Finding Program

a guest
Mar 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. import java.util.*;
  2. import java.math.BigInteger;;
  3. public class RevSqrNmbr {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. boolean bool = true;
  7. while(bool) {
  8. System.out.println("This program will count the number of _-digit reverse square numbers.");
  9. System.out.println("Enter number of digits:");
  10. int dig = sc.nextInt();
  11. while (dig < 1) {
  12. System.out.println("Invalid number of digits. Please enter a valid number.");
  13. dig = sc.nextInt();
  14. }
  15. int count = 0;
  16. for(BigInteger i = BigInteger.TEN.pow(dig - 1); i.compareTo(BigInteger.TEN.pow(dig)) < 0; i.add(BigInteger.ONE)) if(revSq(i)) {
  17. System.out.println(i.toString());
  18. count++;
  19. }
  20. System.out.println("There are " + count + " " + dig + "-digit reverse square numbers");
  21. System.out.println("Continue? Y or N");
  22. String str = sc.next();
  23. if(str.equalsIgnoreCase("n")) bool = false;
  24. else if(!str.equalsIgnoreCase("y")) {
  25. boolean cont = true;
  26. while (cont) {
  27. System.out.println("Invalid answer. Please enter a valid answer.");
  28. str = sc.next();
  29. if(str.equalsIgnoreCase("n")) cont = bool = false;
  30. else if(str.equalsIgnoreCase("y")) cont = false;
  31. }
  32. }
  33. }
  34. }
  35.  
  36. public static boolean revSq(BigInteger i) {
  37. return rev(i.pow(2)) == rev(i).pow(2);
  38. }
  39.  
  40. public static BigInteger rev(BigInteger i) {
  41. return new BigInteger(new StringBuilder(String.valueOf(i)).reverse().toString());
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement