C Program – Check Whether the Input Number is a Neon Number - Digit Sum of Square

Check Whether a Number is Neon

A neon number is defined as a number for which the sum of the digits of its square equals the number itself. For instance, 9 is a neon number because 9² = 81 and 8 + 1 = 9. This program reads an integer, computes its square, adds the digits of the square, and checks if this sum is equal to the original number.

#include <stdio.h>
int main() {
    int n, square, sum = 0, rem;
    printf("Enter an integer: ");
    scanf("%d", &n);
    square = n * n;               // Compute the square
    while (square != 0) {
        rem = square % 10;        // Extract the last digit
        sum += rem;               // Add it to the sum
        square /= 10;             // Remove the last digit
    }
    if (sum == n) {
        printf("%d is a neon number.\n", n);
    } else {
        printf("%d is not a neon number.\n", n);
    }
    return 0;
}
Enter an integer: 9
9 is a neon number.
        

After computing the square, the program extracts each digit of the squared value, adds it to sum, and then checks if this sum equals the original number. If they match, the number satisfies the neon property.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...