fork download
  1. import java.io.*;
  2. class Test {
  3. // Function to check Automorphic number
  4. static boolean isAutomorphic(int N)
  5. {
  6. // Store the square
  7. if(N < 0) N = -N;
  8. int sq = N * N;
  9.  
  10. // Start Comparing digits
  11. while (N > 0) {
  12. // Return false, if any digit of N doesn't
  13. // match with its square's digits from last
  14. if (N % 10 != sq % 10)
  15. return false;
  16.  
  17. // Reduce N and square
  18. N /= 10;
  19. sq /= 10;
  20. }
  21.  
  22. return true;
  23. }
  24.  
  25. // Driver method
  26. public static void main(String[] args)
  27. {
  28. int N = 5;
  29.  
  30. System.out.println(isAutomorphic(N) ? "Automorphic" : "Not Automorphic");
  31. }
  32. }
  33.  
Success #stdin #stdout 0.08s 40852KB
stdin
Standard input is empty
stdout
Automorphic