#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:

  1. Purpose: The program checks if a number is an Automorphic number, meaning its square ends with the same digits as the number itself.
  2. Input: The user enters a number, stored in num.
  3. Calculations:
    • The square of num is calculated.
    • The number of digits in num is counted using a loop.
  4. 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.
  5. Automorphic Check: It compares the extracted last digits with the original number. If they match, it confirms that the number is Automorphic.
  6. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights