# Armstrong Number or Not in C using While loop

Before going to the program first let us understand what is a Armstrong Number?

Armstrong Number:

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

In other word A number is Armstrong if it is equal the sum of cube of its digits.”

For example,

153 is an Armstrong number because 13 + 53 + 33 = 1 + 125 + 27 = 153

124 is not an Armstrong number because 13 + 23 + 43= 1 + 8 + 64 = 73

## Program code for Armstrong Number or Not in C:

```#include<stdio.h>
#include<conio.h>

void main()
{
int n,num,r,ans=0;
clrscr();

printf("Enter a positive integer: ");
scanf("%d", &num);
n=num;

/* Loop to calculate the sum of the cubes of its digits */
while(n>0)
{
r=n%10;
ans=ans+r*r*r;
n=n/10;
}

/* if else condition to print Armstrong or Not */
if(ans==num)
printf("%d is an Armstrong number.",num);
else
printf("%d is not an Armstrong number.",num);
getch();
}
```

## Working:

• First the computer reads the positive integer value from the user.
• Then using while loop it calculates the sum of the cubes of its digits.
• Finally if else condition is used to print the number is Armstrong or Not .

## Step by Step working of the above Program Code:

For Armstrong Number:

1. Let us assume that a user enters the positive integer value as 153.
2. It assigns the value of ans=0, num=153.
3. It assigns the value of n=num(ie. n=153) and the loop continues till the condition of the while loop is true.

3.1.    n>0  (153>0)  while loop condition is true

r=n%10         (r=153%10)       So  r=3

ans=ans+r*r*r     (ans=0+3*3*3)       So  ans=27

n=n/10          (n=153/10)      So  n=15

3.2.    n>0  (15>0)  while loop condition is true

r=n%10         (r=15%10)       So  r=5

ans=ans+r*r*r     (ans=27+5*5*5)       So  ans=152

n=n/10          (n=15/10)      So  n=1

3.3.    n>0  (1>0)  while loop condition is true

r=n%10         (r=1%10)       So  r=1

ans=ans+r*r*r     (ans=152+1*1*1)       So  ans=153

n=n/10          (n=1/10)      So  n=0

3.4.    n>0  (0>0)  while loop condition is false

It comes out of the while loop and checks whether the number is Armstrong or not.

1. ans==num  (153==153)   if condition is true

It prints 153 is an Armstrong number.

1. Thus program execution is completed.

For Not an Armstrong Number:

1. Let us assume that a user enters the positive integer value as 431.
2. It assigns the value of ans=0, num=431.
3. It assigns the value of n=num(ie. n=431) and the loop continues till the condition of the while loop is true.

3.1.    n>0  (431>0)  while loop condition is true

r=n%10         (r=431%10)       So  r=1

ans=ans+r*r*r     (ans=0+1*1*1)       So  ans=1

n=n/10          (n=431/10)      So  n=43

3.2.    n>0  (43>0)  while loop condition is true

r=n%10         (r=43%10)       So  r=3

ans=ans+r*r*r     (ans=1+3*3*3)       So  ans=28

n=n/10          (n=43/10)      So  n=4

3.3.    n>0  (4>0)  while loop condition is true

r=n%10         (r=4%10)       So  r=4

ans=ans+r*r*r     (ans=28+4*4*4)       So  ans=92

n=n/10          (n=4/10)      So  n=0

3.4.    n>0  (0>0)  while loop condition is false

It comes out of the while loop and checks whether the number is Armstrong or not.

1. ans==num  (92==431)   if condition is true

It prints 431 is not an Armstrong number.

1. Thus program execution is completed.

## Output:  1. RAVI says:
2. Aishwarya Joshi says: