From dc65ee7419d81efc2547bf6e64830840482b4f4e Mon Sep 17 00:00:00 2001 From: kvaishvik24 <64711113+kvaishvik24@users.noreply.github.com> Date: Sun, 18 Oct 2020 00:49:23 +0530 Subject: [PATCH] Create Automorphic Number --- Algorithm/Automorphic/Automorphic Number | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Algorithm/Automorphic/Automorphic Number diff --git a/Algorithm/Automorphic/Automorphic Number b/Algorithm/Automorphic/Automorphic Number new file mode 100644 index 0000000..6707d73 --- /dev/null +++ b/Algorithm/Automorphic/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"); + } +}