Sure! An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
Here’s an example of how you can check for Armstrong numbers in C:
#include <stdio.h> #include <math.h> int isArmstrong(int number); int main() { int number; printf("Enter a number: "); scanf("%d", &number); if (isArmstrong(number)) { printf("%d is an Armstrong number.\n", number); } else { printf("%d is not an Armstrong number.\n", number); } return 0; } int isArmstrong(int number) { int originalNumber, remainder, result = 0, n = 0; originalNumber = number; // count the number of digits while (originalNumber != 0) { originalNumber /= 10; ++n; } originalNumber = number; // calculate the result while (originalNumber != 0) { remainder = originalNumber % 10; result += pow(remainder, n); originalNumber /= 10; } // check if the number is Armstrong if (result == number) { return 1; } else { return 0; } }
In this program, we define the isArmstrong
function to check whether a given number is an Armstrong number. It calculates the sum of each digit raised to the power of the number of digits. If the resulting sum is equal to the original number, it returns 1; otherwise, it returns 0.
In the main
function, we take input from the user and call the isArmstrong
function to determine whether the input number is an Armstrong number. Finally, we display the appropriate message based on the result.
Please note that this code assumes that the input number is a non-negative integer.