Here's a C program to find the given number is Armstrong or not with output and proper explanation. This program also uses C while loop and modulus.

What is an Armstrong Number?A number is an Armstrong number if the sum of cubes of each digit of that number is equal to the number itself e.g. 153 is an Armstrong number i.e. 1^{3}+ 5^{3}+ 3^{3}= 153.

# include <stdio.h> # include <conio.h> void main() { int n, r, s = 0, t ; clrscr() ; printf("Enter a number : ") ; scanf("%d", &n) ; t = n ; while(n > 0) { r = n % 10 ; s = s + (r * r * r) ; n = n / 10 ; } if(s == t) printf("\n%d is an armstrong number", t) ; else printf("\n%d is not an armstrong number", t) ; getch() ; }

**Output of above program is**

Enter a number : 153

153 is an armstrong number.

**Explanation of above program**

Following is the list and use of each variable that we're using in this program -

- n - is the number that we're checking.
- t - is the temporary variable whose value is equal to n i.e. t = n.
- r - is used to store the remainder (we'll see that in a minute).
- s - is the sum of cubes of all digits of n.

We first ask the user to enter a number n and then we also store this value of n in temporary variable t. Then using the While loop we calculate sum of cubes of all digits of n. Outside the loop we check if the sum of cubes of all digits of n is equal to the number itself then we print that the number is Armstrong number otherwise the number is not an Armstrong number.

Suppose n = 153. So t is also 153. Let's see how while loop works in this example -

**n = 153 i.e. n > 0. While loop's first iteration -**

- First we calculate value of r -
**r = n % 10**= 153 % 10 = 3. - Next we update the value of sum i.e. s (currently equal to 0) as -
**s = s + (r * r * r) =**0 + (3 * 3 * 3) = 27. - Next we update the value of n by - n = n / 10 = 153 / 10 = 15. (Since n is of data type int only integer value of division is stored in n).

**n = 15 i.e. n > 0. While loop's first iteration -**

- First we calculate value of r -
**r = n % 10**= 15 % 10 = 5. - Next we update the value of sum i.e. s (now equal to 1) as -
**s = s + (r * r * r)**= 27 + (5 * 5 * 5 ) = 27 + 125 = 152. - Next we update the value of n by - n = n / 10 = 15 / 10 = 1.

**n = 1 i.e. n > 0. While loop's first iteration -**

- First we calculate value of r -
**r = n % 10**= 1 % 10 = 1. - Next we update the value of sum i.e. s (now equal to 126) as -
**s = s + (r * r * r) =**152 + (1 * 1 * 1)= 152 + 1 = 153. - Next we update the value of n by - n = n / 10 = 1 / 10 = 0.

n = 0 i.e n < 0. While loop is terminated now. Outside the loop, we compare the value of sum s and variable t and if these two are equal that means the number is Armstrong number.