**Armstrong number program in c** is a program which check whether given number is Armstrong number.

**Armstrong** **number** **program** **in** **c** : In this tutorial we are going to learn Armstrong number program in c. Before that we will see what is mean by Armstrong number, how to know number is Armstrong.

**What is Armstrong number ?**

Armstrong of a number is a sum of Nth power of each digits in number is equal to the number itself. Some Armstrong numbers are 0, 1, 153, 371 etc.

**Formula to find Armstrong number**

Number = Sum of Nth power of digits in number

Now let’s see examples to find Armstrong number

**Example :**

Number = 153

Then sum = 1 ^ 3 + 5 ^ 3 + 3 ^ 3

Sum = 1 + 125 + 27

Sum = 153

So the number is Armstrong number.

**Logic for Armstrong number :**

To find Armstrong number first we take a number. After that we calculate the sum of Nth power each digits. Now we compare sum value and the given number if both are equal then number is Armstrong number in c.

**Concept you require to know from c**

- Header file in c
- Arithmetic Operators in c
- While loop in c

**Algorithm for Armstrong number in c**

- Start
- Declare variables
- Take a number user input
- Next iterate number from end to start using loop
- In loop separate each digital in number, add power of each digits
- Compare number and sum of both are equal then print number is Armstrong
- Print number is Armstrong
- End

## C program for Armstrong number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include<stdio.h> #include<math.h> void main() { int num,i,j,temp,sum=0;; printf("Enter a number to know whether it is armstrong or not\n"); scanf("%d",&num); temp=num; while(num>0) { i=num%10; sum+=i*i*i; num=num/10; } if(sum==temp) { printf("Giveb number %d is an armstrong number\n",temp); } else { printf("Giveb number %d is not an armstrong number since the sum of cubes of individual digits is %d\n",temp,sum); } } |

**Output :**

**Explanation :**

**1. First we declare required header files.**

**2. Start with writing main function.**

**3. Declare variables.**

Num => to store input number

i => to store separated digits.

sum => to store sum

temp => temporary variable

**4. Next copy number to temporary variable.**

temp = num;

So that we can further use temporary variable to check equality to sum.

5. Next line while(num > 0) is to traverse the number from end till we encounter 0.

Suppose for example num = 153

**At iteration 1:**

num = 153 > 0 this condition becomes true and while loop get executed

i=num%10 => i = 153 % 10 = 3

sum+=i*i*i => sum = 0 + 3 * 3 * 3 = 27

num=num/10 => num = 153 / 10 = 15

**At iteration 2:**

num = 15 > 0 this condition becomes true and while loop get executed

i=num%10 => i = 15 % 10 = 5

sum+=i*i*i => sum = 27 + 5 * 5 * 5 = 152

num=num/10 => num = 15 / 10 = 1

**At iteration 3:**

num = 1 > 0 this condition becomes true and while loop get executed

i=num%10 => i = 1 % 10 = 1

sum+=i*i*i => sum = 152 + 1 * 1 * 1 = 153

num=num/10 => num = 1 / 10 = 0

**At iteration 4:**

num = 0 > 0 this condition becomes false and while loop stops it’s execution.

6. Now we have sum = 153 and temp = 153

7. Next we compare temp and sum

if(sum==temp) If this condition is true then print number is Armstrong number.

In our example 153 ==153 => so condition becomes true and if block get executed in block print number is Armstrong number.

**8**. **Else print number is not Armstrong number as it’s sum of Nth power of each digits is not equal.**

**9. End with the Armstrong number program in c.**

## Leave a Reply