In this example, we will write a C Program to check Armstrong number. An Armstrong number (also known as 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, 370 is an Armstrong number
//We are adding cubes of individual digits
//because the number of digits in number is 3
370 = 3*3*3 + 7*7*7 + 0*0*0
= 27 + 343 + 0
= 370
C Program to check if a number is Armstrong number or not
The explanation of the program is available at the end of this code.
#include <stdio.h>
#include <math.h>
int main() {
int number, originalNumber, remainder, numDigits = 0;
double result = 0.0;
// Prompt the user to enter a number
printf("Enter an integer: ");
scanf("%d", &number);
//Copying the number to a different variable
//so that the number doesn't get changed in calculations
originalNumber = number;
// Counting number of digits in the number
while (originalNumber != 0) {
originalNumber /= 10;
++numDigits;
}
originalNumber = number;
// Calculate the sum of the digits raised to the power of numDigits
while (originalNumber != 0) {
//get the last digit
remainder = originalNumber % 10;
//add the last digit raised to the power of number of digits
result += pow(remainder, numDigits);
//remove last digit from number
originalNumber /= 10;
}
// Check if the result is equal to the number
if ((int)result == number)
printf("%d is an Armstrong number.\n", number);
else
printf("%d is not an Armstrong number.\n", number);
return 0;
}
Output:
Enter an integer: 153
153 is an Armstrong number.
Explanation of the program:
- Include header files:
stdio.h
is included forscanf
andprintf
.math.h
is included for thepow
function which calculates the power of a number.
- Declare variables:
number
to store the number entered by user.originalNumber
to hold the copy ofnumber
for calculation purposes.remainder
to get the last digit of number on every iteration.numDigits
to store the number of digits in the number.result
to store the sum of each digit raised to the power of the number of digits.
- Input:
- Prompt the user to enter a number using
printf
and then usescanf
to read & store the entered value.
- Prompt the user to enter a number using
- Calculate the number of digits:
- Dividing number by 10 (which removes last digit from number) and increasing the value of
numDigits
by 1 on every iteration.
- Dividing number by 10 (which removes last digit from number) and increasing the value of
- Calculate the Armstrong number:
- Get last digit of number using modulo % operator on every iteration of while loop.
- Raise each digit to the power of
numDigits
usingpow
function and add toresult
. - Divide the number by 10 to remove last digit so that the second last digit gets processed on next iteration.
- Check and output the result:
- Compare the original number with the calculated result, if equal, print that the input number is an Armstrong number.
- Otherwise, print that the input number is not an Armstrong number.
Leave a Reply