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:-
- Calculate the square of the number
- Extract digits of the square from the end
- If end digits and the number become equal at some point
- 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
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Login/Signup to comment
// 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;
}