Last Updated on May 11, 2023 by Prepbytes

This article discusses a program that can determine whether a given number is an automorphic number or not.

## What is an Automorphic Number?

An automorphic number is one that has a square that ends in the same digits as the number itself. Let us illustrate this with an example.

**For example,**

If you are given an integer, say N, with the value 25, i.e., N = 25, the square of N is 625. As a result, N = 25 is an automorphic number because the square ends in the same digits as the number N.

## Finding whether a Number is an Automorphic Number or Not in C

You are given the number N, and your task is to determine whether or not it is an automorphic number. In our introduction, we discussed what an automorphic number is and provided an example. Now, let’s quickly discuss how we can approach this problem.

### Approach 1 for Finding whether a Number is an Automorphic Number or Not in C

- Keep track of the square of the given number.
- Loop until N equals 0 because we need to match all digits with their squares.
- Check whether (n%10 == sq%10), that is, whether the last digit of the number is the last digit of the square, is true. If not equal, return false.
- Otherwise, proceed by reducing the number and square, i.e., n = n/10 and sq = sq/10;
- If all of the digits match, return true.

**Implementation of the above approach in C:**

#include <stdio.h> #include <stdbool.h> bool isAutomorphic(int N) { if (N < 0) N = -N; // Store the square int sq = N * N; while (N > 0) { if (N % 10 != sq % 10) return false; // Reduce N and square N /= 10; sq /= 10; } return true; } int main() { int N = 5; isAutomorphic(N) ? printf("Automorphic") : printf("Not Automorphic"); return 0; }

**Output:**

`Automorphic `

**Time Complexity:** O(log10N)

### Approach 2 for Finding whether a Number is an Automorphic Number or Not in C

- Check to see if the number is negative, then make it positive.
- Save the number’s square.
- Find the count of the digit of the number so that you can find the count of the digit of the last number of the square of the number equal to the number; note that this does not imply that if the count of the last number of the square is equal to the number, they will be equal.
- After counting the digits of the number, perform: squareNum%power(10, count)
- Finally, check to see if the number’s last square is equal to the number.

**Implementation of the above approach in C:**

#include <stdio.h> #include <math.h> #include <stdbool.h> 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)) { printf("Automorphic"); } else { printf("Not Automorphic"); } printf("\n"); return 0; }

**Output:**

`Not Automorphic`

**Time Complexity:** O(log10N)

**Conclusion**

This article defines an automorphic number and gives an example. An automorphic number is one whose square has the same digits as the number. The article describes two approaches in the C programming language for determining whether or not a given number is an automorphic number. The first method proposes keeping track of the given number’s square, looping until the number equals 0, checking if the last digit of the number matches the last digit of the square, reducing the number and square, and returning true if all digits match. The second method involves determining whether the number is negative, saving the number’s square, determining the count of the digit of the number to determine the count of the digit of the last number of the square, performing squareNum%power(10, count), and determining whether the number’s last square is equal to the number. Both approaches have a time complexity of O(log10N).

## Frequently Asked Questions (FAQs)

**Q1. How can you tell if a number is automorphic or not?**

**Ans.** An automorphic number is one with the same digits at the end as the number itself.

**Q2. What is an illustration of an automorphic number?**

**Ans.** As (25)2 = 625 and (76)2 = 5776, an automorphic number is an integer whose square ends with the given integer.

**Q3. How do I get an automorphic number to print?**

**Ans.** First, check to see if the number’s last square is equal to the number.If it meets the condition, you can print it and call it an automorphic number.