#include <stdio.h>
int main() {
int num, square, digits, lastDigits;
// Asking the user to input a number
printf("Enter a number: ");
scanf("%d", &num);
// Calculate the square of the number
square = num * num;
// Determine the number of digits in the original number
digits = 0;
int temp = num;
while (temp != 0) {
temp /= 10;
digits++;
}
// Extract the last digits from the square equal to the number of digits in the original number
lastDigits = square % (int)pow(10, digits);
// Check if the last digits of the square equal the original number
if (lastDigits == num) {
printf("%d is an Automorphic number.\n", num);
} else {
printf("%d is not an Automorphic number.\n", num);
}
return 0;
}
Explanation:
Purpose: The program checks if a number is an Automorphic number, meaning its square ends with the same digits as the number itself.
Input: The user enters a number, stored in num.
Calculations:
The square of num is calculated.
The number of digits in num is counted using a loop.
Extract Last Digits: The program extracts the last digits of the square using the modulus operation with 10number of digits10^{\text{number of digits}}10number of digits.
Automorphic Check: It compares the extracted last digits with the original number. If they match, it confirms that the number is Automorphic.
Output: The program prints whether the number is Automorphic or not.
Output:
1. Enter a number: 5
5 is an Automorphic number.
2. Enter a number: 30
30 is not an Automorphic number.