C Program to check if a number is armstrong
Learn how to write a C program to check if a number is an Armstrong number. This guide provides a detailed explanation and complete code example.
An Armstrong number, also known as a narcissistic number or a pluperfect 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, the number 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
In this article, we will write a C program to check whether a given number is an Armstrong number or not. We'll start by understanding the concept of Armstrong numbers in more detail, then outline the algorithm, and finally provide the complete C program with an explanation.
Understanding Armstrong Numbers
An Armstrong number of n digits is an integer such that the sum of its digits raised to the n-th power is equal to the number itself. For example:
- 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
- 9474 is an Armstrong number because 9^4 + 4^4 + 7^4 + 4^4 = 9474.
Steps to Check for an Armstrong Number
- Input the number: Get the number from the user.
- Count the digits: Determine the number of digits in the number.
- Calculate the sum of powers: Compute the sum of each digit raised to the power of the number of digits.
- Compare the sum with the original number: If they are equal, it is an Armstrong number; otherwise, it is not.
Write a C Program to check if a number is armstrong
Here's the C program to check whether a given number is an Armstrong number or not:
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, result = 0, n = 0;
// Input the number from the user
printf("Enter an integer: ");
scanf("%d", &num);
// Store the original number to compare later
originalNum = num;
// Count the number of digits in the number
int temp = num;
while (temp != 0) {
temp /= 10;
n++;
}
// Calculate the sum of the nth powers of each digit
temp = num;
while (temp != 0) {
remainder = temp % 10; // Extract the last digit
result += pow(remainder, n); // Raise the digit to the power of n and add to result
temp /= 10; // Remove the last digit
}
// Check if the number is an Armstrong number
if (result == num) {
printf("%d is an Armstrong number.", num);
} else {
printf("%d is not an Armstrong number.", num);
}
return 0;
}
Output
Enter an integer: 153
153 is an Armstrong number.
Explanation of the Code
- Input the Number:
scanf("%d", &num);
reads the integer input from the user.
- Store the Original Number:
originalNum = num;
saves the original number to compare later.
- Count the Number of Digits:
- A
while
loop (while (temp != 0)
) iterates through the number, dividing by 10 each time to count the digits. The loop runs untiltemp
becomes 0. n++
increments the digit count in each iteration.
- A
- Calculate the Sum of Powers:
- The
while
loop (while (temp != 0)
) continues until all digits are processed. remainder = temp % 10;
extracts the last digit oftemp
.result += pow(remainder, n);
adds the digit raised to the power ofn
toresult
.temp /= 10;
removes the last digit fromtemp
.
- The
- Check for Armstrong Number:
- The condition
if (result == num)
checks if the sum of the digits raised to the power of the number of digits equals the original number. If true, it prints that the number is an Armstrong number; otherwise, it states that it is not.
- The condition
Example Runs
Let's look at some example runs to see how the program works:
Example 1
Enter an integer: 153
153 is an Armstrong number.
- Calculation: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Example 2
Enter an integer: 123
123 is not an Armstrong number.
- Calculation: 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36
Example 3
Enter an integer: 9474
9474 is an Armstrong number.
- Calculation: 9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 9474
If you found this article helpful, you might also be interested in learning how to calculate the power of a number. Check out our detailed guide below: