BAEL-5385 Code for Automorphic Number. (#11928)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.automorphicnumber;
|
||||
|
||||
public class AutomorphicNumber {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(isAutomorphicUsingLoop(76));
|
||||
System.out.println(isAutomorphicUsingMath(76));
|
||||
}
|
||||
|
||||
public static boolean isAutomorphicUsingMath(int number) {
|
||||
int square = number * number;
|
||||
|
||||
int numberOfDigits = (int) Math.floor(Math.log10(number) + 1);
|
||||
int lastDigits = (int) (square % (Math.pow(10, numberOfDigits)));
|
||||
|
||||
return number == lastDigits;
|
||||
}
|
||||
|
||||
public static boolean isAutomorphicUsingLoop(int number) {
|
||||
int square = number * number;
|
||||
|
||||
while (number > 0) {
|
||||
if (number % 10 != square % 10) {
|
||||
return false;
|
||||
}
|
||||
number /= 10;
|
||||
square /= 10;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user