If you are new to C++ programming, learning small number-based programs is a great way to strengthen your logic and coding skills. An interesting concept in number theory is an Automorphic number—a number whose square ends with the same digits as the number itself. For example, 5 is an Automorphic number because $5^2 = 25$, and 25 ends with 5. Understanding how to check Automorphic numbers in C++ helps beginners practice loops, modular arithmetic, and conditional statements, all essential building blocks for more complex programs.

with hands-on learning.
get the skills and confidence to land your next move.
Program 1: Check Automorphic Number Using User Input
This program lets the user enter a number and checks whether it is an Automorphic number. It’s a simple way to understand how input, arithmetic operations, and conditional logic work together in C++.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: " << endl;
cin >> number;
int square = number * number;
int temp = number;
bool isAutomorphic = true;
while(temp > 0) {
if(temp % 10 != square % 10) {
isAutomorphic = false;
break;
}
temp /= 10;
square /= 10;
}
if(isAutomorphic)
cout << number << " is an Automorphic number." << endl;
else
cout << number << " is not an Automorphic number." << endl;
return 0;
}
In this program, the user enters a number, and the program calculates its square. The digits of the number and its square are compared from the least significant digit using the modulo operator. If all digits match, the number is Automorphic. Beginners can see how loops and conditional statements allow them to check complex conditions step by step.
Program 2: Check Automorphic for Predefined Number
Sometimes you may want to check a specific number automatically, without user input. This program shows how to check a single predefined number for the Automorphic property.
#include <iostream>
using namespace std;
int main() {
int number = 76; // predefined number
int square = number * number;
int temp = number;
bool isAutomorphic = true;
while(temp > 0) {
if(temp % 10 != square % 10) {
isAutomorphic = false;
break;
}
temp /= 10;
square /= 10;
}
if(isAutomorphic)
cout << number << " is an Automorphic number." << endl;
else
cout << number << " is not an Automorphic number." << endl;
return 0;
}
Here, we assign the number directly to a variable and use the same logic to check its square. Beginners can practice modifying the value to see how the program behaves with different numbers. It’s useful for quick tests or automated checks.
Program 3: Check Automorphic Number for Predefined Values
When you want to check several numbers at once, an array makes the task easier. This program demonstrates checking multiple predefined numbers for Automorphic property.
#include <iostream>
using namespace std;
bool isAutomorphic(int number) {
int square = number * number;
int temp = number;
while(temp > 0) {
if(temp % 10 != square % 10) {
return false;
}
temp /= 10;
square /= 10;
}
return true;
}
int main() {
int numbers[] {5, 6, 25, 76, 13};
int n = sizeof(numbers) / sizeof(numbers[0]);
for(int i = 0; i < n; i++) {
if(isAutomorphic(numbers[i]))
cout << numbers[i] << " is an Automorphic number." << endl;
else
cout << numbers[i] << " is not an Automorphic number." << endl;
}
return 0;
}
This version uses a function isAutomorphic()
to check each number in an array. Beginners can see how to structure code with functions and arrays, making it reusable and organized.
Frequently Asked Questions (FAQ)
Here are some common questions beginners might have about Automorphic numbers in C++.
Q1: What exactly is an Automorphic number?
A1: An Automorphic number is a number whose square ends with the same digits as the number itself. For example, 5 and 76 are Automorphic numbers because $5^2 = 25$ and $76^2 = 5776$.
Q2: Can negative numbers be Automorphic?
A2: Generally, Automorphic numbers are considered positive integers. Negative numbers can theoretically be checked, but it is uncommon in number theory practice.
Q3: What C++ concepts are used in checking Automorphic numbers?
A3: Key concepts include loops, conditional statements, modulo (%) operator, and functions.
Q4: How do we compare the digits of a number and its square?
A4: We repeatedly take the last digit of both the number and its square using modulo 10, compare them, and then remove the last digit by dividing by 10.
Q5: Why use functions for predefined numbers?
A5: Functions make the code reusable and easier to read. You can check multiple numbers without duplicating logic.
Conclusion
Checking Automorphic numbers in C++ is a fun and practical way to learn loops, conditionals, and modular arithmetic. By experimenting with user input, a single predefined number, and multiple predefined numbers, beginners can understand different ways to structure code. Practicing these programs improves problem-solving skills and prepares you for more advanced number-based programs. Keep experimenting with different numbers and variations to strengthen your C++ skills.
Additional & References
Beginners are encouraged to explore variations of these programs, such as checking all Automorphic numbers within a range or using recursion. Practicing these small programs helps build confidence and a strong foundation in C++.
- C++ Official Documentation – Complete reference for syntax and libraries.
- TutorialsPoint C++ Tutorial – Beginner-friendly tutorials with examples.
- GeeksforGeeks C++ Programming – Practical tips, exercises, and sample programs.