What is an Armstrong Number?
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. Similarly, 9474 is an Armstrong number because 9^4 + 4^4 + 7^4 + 4^4 = 9474.
An n-digit number is called an Armstrong number if the sum of cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number as 153 = 111 + 555 + 333.
Approach to check Armstrong number in C language
Here is an approach to check if a number is an Armstrong number in C programming language in point form:
- Take input from the user: Prompt the user to enter a number and store it in a variable.
- Find the number of digits: Use a while loop to count the number of digits in the given number.
- Separate the digits: Use another while loop to separate the digits of the number. In each iteration, take the remainder of the number when divided by 10 and store it in a variable.
- Raise each digit to the power of the number of digits: Use the pow() function from the math.h library to raise each digit to the power of the number of digits. Keep a separate variable to keep track of the sum of these values.
- Compare the sum with the original number: If the sum is equal to the original number, it is an Armstrong number. If not, it is not an Armstrong number.
- Print the result: Print whether the given number is an Armstrong number or not.
Our approach is to separate the digits of the number, raise each digit to the power of the number of digits, and add the resulting values. If the sum is equal to the original number, it is an Armstrong number.
Pseudo code to check Armstrong number in C language.
int number, originalNumber, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += remainder * remainder * remainder;
originalNumber /= 10;
}
if (result == number)
printf("%d is an Armstrong number.", number);
else
printf("%d is not an Armstrong number.", number);
This code prompts the user to enter a three-digit integer, then uses a while loop to find the sum of the cubes of each digit. If the sum is equal to the original number, it’s an Armstrong number; if not, it’s not an Armstrong number.
C program to check Armstrong number
Here’s a C program that checks if a number is an Armstrong number:
#include <stdio.h>
#include <math.h>
int main() {
int number, originalNumber, remainder, result = 0, n = 0;
printf("Enter a number: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0) {
originalNumber /= 10;
n++;
}
originalNumber = number;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += pow(remainder, n);
originalNumber /= 10;
}
if (result == number)
printf("%d is an Armstrong number.", number);
else
printf("%d is not an Armstrong number.", number);
return 0;
}
This program first takes input from the user then using while loop it separates the digits of the number and then using another while loop it uses pow function to find the power of the each digit by the number of digits. At the end it compares if the sum is equal to the original number, If it is equal then it is an Armstrong number otherwise it is not. Please note that this program uses the math.h library, which must be included at the top of the file in order to use the pow() function.
You can also check the Other C Programs :