Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Automorphic Number
Next article icon

Automorphic Number

Last Updated : 20 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

An automorphic number is a number whose square ends in the same digits as the number itself. A number n is called automorphic if:

n2 (mod  10d) = n

where d is the number of digits in n.

Examples:

  • n = 5:
    • n2 = 25
    • Last digit of 25 is 5, so 5 is automorphic.
  • n = 76:
    • n2 = 5776
    • Last two digits of 5776 are 76, so 76 is automorphic.
  • n = 6:
    • n2 = 36
    • Last digit of 36 is 6, so 6 is automorphic.
  • n = 25:
    • n2 = 625
    • Last two digits of 625 are 25, so 25 is automorphic.

Given a number N, the task is to check whether the number is an Automorphic number or not. A number is called an Automorphic number if and only if its square ends in the same digits as the number itself.

Examples : 

Input  : N = 76 
Output : Automorphic
Explanation: As 76*76 = 5776

Input  : N = 25
Output : Automorphic
As 25*25 = 625

Input : N = 7
Output : Not Automorphic
As 7*7 = 49

Approach:

  • Store the square of given number.
  • Loop until N becomes 0 as we have to match all digits with its square.
    • Check if (n%10 == sq%10) i.e. last digit of number = last digit of square or not
      • if not equal, return false.
      • Otherwise, continue i.e. reduce the number and square i.e. n = n/10 and sq = sq/10;
  • Return true if all digits matched.

Below is the implementation of the above approach: 

C++
// C++ program to check if a number is Automorphic #include <iostream> using namespace std;  // Function to check Automorphic number bool isAutomorphic(int N) {            if(N < 0) N = -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 code int main() {     int N = 5;      isAutomorphic(N) ? cout << "Automorphic"                      : cout << "Not Automorphic";      return 0; } 
Java
// Java program to check if a number is Automorphic import java.io.*; class Test {     // Function to check Automorphic number     static boolean isAutomorphic(int N)     {         // Store the square           if(N < 0) N = -N;         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");     } } 
Python
# Python program to check if a number is Automorphic   # Function to check Automorphic number def isAutomorphic(N):      # Store the square     if N < 0:       N = -N     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 code N = 5 if isAutomorphic(N) :     print ("Automorphic") else :     print  ("Not Automorphic")       # This Code is contributed by Nikita Tiwari. 
C#
// C# program to check if a // number is Automorphic using System;  class GFG {      // Function to check Automorphic number     static bool isAutomorphic(int N)     {          // Store the square           if(N < 0) N = -N;         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 Code     public static void Main()     {         int N = 5;          Console.Write(isAutomorphic(N) ? "Automorphic" : "Not Automorphic");     } }  // This code is Contributed by Nitin Mittal. 
JavaScript
<script>  // Javascript program to check if // a number is Automorphic    // Function to check // Automorphic number function isAutomorphic(N) {     // Store the square     if(N < 0) N = -N;     let 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 -1;            // Reduce N and square         N /= 10;         sq /= 10;     }        return 1; }    // Driver code let N = 5;    let geeks = isAutomorphic(N) ?               "Automorphic" :            "Not Automorphic";     document.write(geeks);    // This code is contributed by _saurabh_jaiswal </script> 
PHP
<?php // PHP program to check if // a number is Automorphic  // Function to check // Automorphic number function isAutomorphic($N) {     // Store the square       if($N < 0) $N = -$N;     $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 -1;          // Reduce N and square         $N /= 10;         $sq /= 10;     }      return 1; }  // Driver code $N = 5;  $geeks = isAutomorphic($N) ?               "Automorphic" :            "Not Automorphic";     echo $geeks;  // This code is contributed by ajit ?> 

Output
Automorphic

Time Complexity: O(log10N)
Auxiliary Space: O(1)

Another Approach to Solve the Problem

  1. Do first check if number is negative then make it positive.
  2. Store the square of number.
  3. Find the count of the digit of the number sothat you can find the count of digit of last number of the square of the number equal to the number i.e it doesn't mean if the count of digit of last number of square is equal to the number will be equal to each other.
  4. And after counting the digit of the number perform : -  squareNum%power(10, count)
  5. Finally check the last number of square of number is equal to number or not.

Let's see the implementation as explained for above approach : -

C++
#include <iostream> #include <math.h> using namespace std; bool checkAuto(int a){   if(a < 0) a = -a;     int squareNum = a*a;     int temp = a;     int count = 0;   // count of digit of a     int lastNum = 0;     while(temp > 0){         count++;         temp = temp/10;     }     int lastDigit = (squareNum)%(int(pow(10, count)));     if(lastDigit == a) return true;     else return false; } int main() {   int num = -4;   if(checkAuto(num)) cout << "Automorphic";   else cout << "Not Automorphic";   cout << endl;   return 0; } 
Java
import java.io.*; import java.util.*; public class Solution {         public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int a = -4;         if(a < 0) a = -a;         int squareNum = a*a;         int temp = a;         int count = 0;   // count of digit of a         while(temp > 0){             count++;             temp = temp/10;         }         int lastDigit = squareNum%(int)Math.pow(10, count);         // System.out.print(lastDigit);         if(lastDigit == a) System.out.print("Automorphic");         else System.out.print("Not Automorphic");              } } 
Python
def checkAuto(a):     if a < 0: a = -a     squareNum = a*a     temp = a     count = 0     while temp != 0:         count += 1         temp = int(temp/10)     lastDigit = squareNum%pow(10, count)     if lastDigit == a:         return "Automorphic"     else:          return "Not Automorphic" num = -4 print(checkAuto(num)) 
C#
using System;  class Solution {     static void Main(string[] args)     {         int a = -4;         if (a < 0)             a = -a;         int squareNum = a * a;         int temp = a;         int count = 0; // count of digit of a         while (temp > 0) {             count++;             temp = temp / 10;         }         int lastDigit             = squareNum % (int)Math.Pow(10, count);         // Console.Write(lastDigit);         if (lastDigit == a)             Console.Write("Automorphic");         else             Console.Write("Not Automorphic");     } } 
JavaScript
function checkAuto(a){   if(a < 0) a = -a;     let squareNum = a*a;     let temp = a;     let count = 0;   // count of digit of a     while(temp > 0){         count++;         temp = Math.floor(temp/10);     }     let lastDigit = (squareNum)%(Math.pow(10, count));     if(lastDigit == a) return 1;     else return 0; } let num = -4; if(checkAuto(num)) console.log("Automorphic"); else console.log("Not Automorphic"); 

Output
Not Automorphic

Time Complexity: - O(log10N), where N is the given number.
Auxiliary Space:- O(1)



Next Article
Automorphic Number

S

Sahil Chhabra
Improve
Article Tags :
  • Mathematical
  • DSA
Practice Tags :
  • Mathematical

Similar Reads

    Mathematical Algorithms - Number Digits
    "Mathematical Algorithms | Number Digits" is a guide that explores problems and solutions involving digits. It covers common digit-related issues, such as digit properties, manipulation, and counting under constraints. The article discusses algorithmic approaches like modular arithmetic, string hand
    9 min read
    Self Numbers
    A Number N is said to be Self Number if it can not be written as M + sum of digits of M for any M.The first few Self numbers are: 1, 3, 5, 7, 9, 20, 31, 42................ Check if N is a Self number Given an integer N, the task is to find if this number is Self number or not. Examples: Input: N = 3
    5 min read
    Special two digit number
    A special two-digit number is a number such that when the sum of the digits of the number is added to the product of its digits, the result is equal to the original two-digit number. Examples : input : 59. output : 59 is a Special Two-Digit Number Explanation: Sum of digits = 5 + 9 = 14 Product of i
    6 min read
    POTD Solutions | 10 Nov’ 23 | Number following a pattern
    Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Stack and Two Pointer Algorithm but will also help you build up problem-solving
    6 min read
    Game of Numbers - Playing with Numbers | Class 8 Maths
    We use numbers in our day-to-day life. We buy everything with money and measure its quantity with the help of Numbers only. Therefore Numbers play a very significant role in our life. The common Representation of a Number is as follows: The general form of a two-digit number is ab=(10 × a)+b Here ab
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences