C Program to find Armstrong numbers

What are Armstrong numbers?

Armstrong number is a 3-digit number such that the sum of the cube of each of its digits is equal to the number itself.

For example:

The following is a C program find all the Armstrong numbers between 100 and 999:

Expected Output:

How it works:

The following table demonstrates what happens at each iteration of the while loop (assuming the n = 153):

Iteration rem sum num
After 1st iteration rem = 153%10 = 3 sum = 0 + 3^3 = 27 num = 153 / 10 = 15
After 2nd iteration rem = 15%10 = 5 sum = 27 + 5^3 = 152 num = 15 / 10 = 1
After 3rd iteration rem = 1%10 = 1 sum = 152 + 1^3 = 153 num = 1 / 10 = 0

Recommended Reading:

Leave a Comment

%d bloggers like this: