Armstrong Number in C
Here, you will get and learn the program code of Armstrong number in c programming with algorithm, program code, and complete explanation.
What is Armstrong number in C?
A number is said to be an Armstrong number if it equals the sum of its own digits when each is raised to the power of the number of digits. For example 153 is an Armstrong number, 153= (1*1*1)+(5*5*5)+(3*3*3)
Logic of Armstrong
The logic of Armstrong number can be done into following steps:
1. Take a number as input.
2. Break the number into its individual digits.
3. Cube each digit.
4. Add the cubes of all the digits.
5. If the total equals the number, it is an Armstrong number.
Steps with Example
Step 1. 153
Step 2. 1,5,3
Step 3. (1*1*1)+(5*5*5)+(3*3*3)
Step 4. 1+125+27
Step 5. 153
So, 153 is an Armstrong Number
Algorithm of Armstrong Number in C
● Take the number of digits in the number
● Store the number in a variable
● Initialize a variable sum to 0
● Using a loop, iterate over each digit in the number
● For each digit, calculate the power of the number using the number of digits as the exponent
● Add the power to the sum
● Compare the sum with the number
● If the sum is equal to the number, the number is an Armstrong number
● Print the result
Armstrong Number in C program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //Program of Armstrong Number in C #include<stdio.h> int main() { int num, originalNum, remainder, result = 0; printf("Enter a three-digit integer: "); scanf("%d", &num); originalNum = num; while (originalNum != 0) { remainder = originalNum % 10; result += remainder * remainder * remainder; originalNum /= 10; } if (result == num) printf("%d is an Armstrong number.",num); else printf("%d is not an Armstrong number.",num); return 0; } |
Output
Check out our other C programming Examples