diff --git a/Task3/java/Automorphic Number b/Task3/java/Automorphic Number new file mode 100644 index 00000000..770d201b --- /dev/null +++ b/Task3/java/Automorphic Number @@ -0,0 +1,42 @@ +Given a number N, the task is to check whether the number is Automorphic number or not. +A number is called Automorphic number if and only if its square ends in the same digits as the number itself. + +Input : N = 25 +Output : Automorphic +As 25*25 = 625 + +Input : N = 7 +Output : Not Automorphic +As 7*7 = 49 + +SOLUTION: +class Test { + // Function to check Automorphic number + static boolean isAutomorphic(int N) + { + // Store the square + int sq = N * N; + + // Start Comparing digits + while (N > 0) { + // Return false, if any digit of N doesn't + // match with its square's digits from last + if (N % 10 != sq % 10) + return false; + + // Reduce N and square + N /= 10; + sq /= 10; + } + + return true; + } + + // Driver method + public static void main(String[] args) + { + int N = 5; + + System.out.println(isAutomorphic(N) ? "Automorphic" : "Not Automorphic"); + } +}