Automorphic Number program in C++

In this article, we will write a C++ Program for Automorphic Number. The following C++ programming topic is used in the program below.

A number is said to be an automorphic number if the square of the given number ends with the same digits as the number itself. Example: 25, 76, 376, etc.

Automorphic Number

Automorphic Number Program in C++

The C++ program to check automorphic number below takes the number from the user as input and checks accordingly.

#include <iostream>
using namespace std;

int main()
{
  int num, flag = 0, sqr;

  //user input
  cout << "Enter the number: ";
  cin >> num;

  sqr = num * num;
  int temp = num;

  //calculation
  while (temp > 0)
  {
    if (temp % 10 != sqr % 10)
    {
      flag = 1;
      break;
    }

    temp /= 10;
    sqr /= 10;
  }

  if (flag == 1)
    cout << num << ": Not an Automorphic number.";
  else
    cout << num << ": Automorphic number.";
  return 0;
}

Output:

Enter the number: 25
25: Automorphic number.

//second execution
Enter the number: 95
95: Not an Automorphic number.