Java while loop find automorphic number

Description

Java while loop find automorphic number

public class Main {
  public static void main(String[] args) {
    System.out.println(376 + " is automorphic? : " + isAtomorphic(376));
    System.out.println(25 + " is automorphic? : " + isAtomorphic(25));
  }//from   w ww .j a va 2  s  . c om

  public static boolean isAtomorphic(int n) {
    int square = n * n;
    while (n > 0) {
      int d1 = n % 10;
      int d2 = square % 10;
      if (d1 != d2) {
        return false;
      }
      n = n / 10;
      square = square / 10;
    }
    return true;
  }
}



PreviousNext

Related