This example shows you how to find the entered number is Armstrong number or not in C. Very useful program for job seekers and school / college students.
I have already written the same program in PHP, Python, C# and Java, please use “Armstrong” keyword to find the programs if you wish to.
Here is the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> int main() { int num, sum = 0, rem = 0, temp; printf("Enter the Number: "); //get the number scanf("%d", &num); //store it in a temp variable temp = num; //loop till temp becomes 0 while (temp != 0) { rem = temp % 10; //find the reminder sum = sum + (rem * rem * rem); //cube reminder and add it to sum temp = temp / 10; //find quotient, if not 0 then loop again } if (num == sum) //if original value of num and found value of sum is equal then it is Armstrong { printf("Armstrong Number \n"); } else { printf("Not an Armstrong Number \n"); } return 0; } |
The logic has three steps,
1. Find the reminder and cube the reminder.
2. Add it to the variable sum.
3. Find the quotient for the next loop.
If both the entered number and the value of sum variable is equal then the number is Armstrong Number.
153, 371, 407 are the Armstrong numbers.