# C++ Program to check whether a number is an Automorphic number or not

## Automorphic  Number in C++

In this post, we will learn to code Automorphic Number in C++

An Automorphic number is a special number, whose’s square ends with the same digits as the number itself

## Algorithm:-

1. Calculate the square of the number
2. Extract digits of the square from the end
3. If end digits and the number become equal at some point
4. Then its an automorphic number

### C++ Code:-

Run
```#include <iostream>
using namespace std;

int isAutomorphic(int n){

int square = n * n;

while(n != 0)
{
// means not automorphic number
if(n % 10 != square % 10){
return 0;
}

// reduce down numbers
n /= 10;
square /= 10;
}
// if reaches here means automorphic number
return 1;
}

int main ()
{
int n = 376, sq = n * n ;

if(isAutomorphic(n))
cout << "Num: "<< n << ", Square: " << sq << " - is Automorphic";
else
cout << "Num: "<< n << ", Square: " << sq << " - is not Automorphic";

}
// Time complexity: O(N)
// Space complexity: O(1)
```

### Output

`Num: 376, Square: 141376 - is Automorphic`

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

### One comment on “C++ Program to check whether a number is an Automorphic number or not”

• Abhay

// to check number is automorphic or not
#include
using namespace std;
int main()
{
int a,i=1;
cin>>a;
int sqr=a*a;
while(i<a)
{
i=i*10;
}
if(sqr%i==a)
{cout<<"\nno is automorphic";}
else
{cout<<"\nno is not automorphic";}
return 0;
}