C++ Program to check whether a number is an Automorphic number or not
Program to check whether a number is an Automorphic number or not
Here we will discuss how to check whether a number is an Automorphic number or not using C++ programming language.
An automorphic number is a number in which the last digits of the square of the number is equal to the number itself.
To check for Automorphic number, first find the square of the number. Then compare the last digits of the square by the number digit by digit and then are equal then it’s an Automorphic number else not.
Square of 25 = 625
As we can see the last digits of square equals to the number, so 25 is an Automorphic number.
Let’s see how the code works:
- User gives an input(number).
- Input is stored in an int type variable say num.
- The square of num is found and stored in another variable say sq
- num is stored in another variable called store for future use.
- A loop is started
- If the last digit of the num and sq are not equal then turn flag = 1 and break the loop
- Divide both num and sq by 10.
- The loop will run till num is greater than 0.
- If flag is equal to 1 then the number is not an automorphic number.
- Otherwise the number is an Automorphic number.
//Automorphic number or not
using namespace std;
cout<<"Enter a positive number to check: ";
int sq= num*num;
//check for automorphic number
cout<<store<<" is not an Automorphic number.";
cout<<store<<" is an Automorphic number.";
Enter a positive number to check: 25
25 is an Automorphic number.