**Armstrong numbers between 1 to 1000** is a program to print all the possible number between 1 to 1000 which are known as Armstrong numbers.

In a previous tutorial, we learned a program to check number is Armstrong number or not.

### What is Armstrong number?

Armstrong of a number is a sum of Nth power of each digit in a 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

**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 digit. now we compare the sum value and the given number if both are equal then the number is Armstrong number.

## C program for armstrong number between 1 to 1000

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include<stdio.h> #include<math.h> main() { int num,i,j,temp1,temp2,sum=0; printf("Enter a number to know all armstrong number between them\n"); scanf("%d",&num); printf("Armstrong numbers between 1 to %d are:\n",num); for(i=1;i<=num;i++) { sum=0; temp1=i; temp2=i; while(temp1>0) { j=temp1%10; sum+=pow(j,3); temp1=temp1/10; } if(sum==temp2) { printf("%d\n",sum); } } } |

**Output :**

**Explanation :**

**1. Start with the writing the library files in case as first line. We include 2 header files stdio.h for standard input and output and math.h for performing mathematics operation.**

**2. Write the main function from where execution of program starts.**

**3. Declare the variables.**

int num => to store N value input

i,j,temp1,temp2 => temporary variable

sum => to store sum

**4. Take user input which is a last number till user want to print Armstrong numbers.**

Suppose for example : num = 1000

**5. Now the main logic begins**

for(i=1;i<=num;i++) this is to iterate a loop for printing a Armstrong number from 1 to N.

**For loop At iteration 1 :**

i = 1 , 1 <= 1000 condition becomes true for loop gets executed

sum=0 => sum = 0

temp1=i => temp1= 1

temp2=i => temp2 = 1

Now write a while loop to calculate sum of power of nth digits

**While loop iteration 1**

while(temp1>0) => 1 > 0

{

j=temp1%10 => j = 1 % 10 = 1

sum+=pow(j,3) => sum = pow (1,3) = 1

temp1=temp1/10 => temp1 = 1/10 = 0

}

**While loop iteration 2**

while(temp1>0) => 0 > 0 this condition become false so it come out the loop

Next week check sum and temp2 in if

if(sum==temp2) => 1 == 1 this condition become true that is number is Armstrong so we print that number.

{

printf(“%d\n”,sum) => 1

}

**For loop At iteration 2 :**

i = 2 , 2 <= 1000 condition becomes true for loop gets executed

sum=0 => sum = 0

temp1=i => temp1= 2

temp2=i => temp2 = 2

Now write a while loop to calculate sum of power of nth digits

**While loop iteration 1**

while(temp1>0) => 2 > 0

{

j=temp1%10 => j = 2 % 10 = 2

sum+=pow(j,3) => sum = pow (2,3) = 8

temp1=temp1/10 => temp1 = 2/10 = 0

}

**While loop iteration 2**

while(temp1>0) => 0 > 0 this condition become false so it come out the loop

Next week check sum and temp2 in if

if(sum==temp2) => 8 == 2 this condition become false so If condition get skipped.

**6. This process continues till num = 1000.**

**7. At the end we got all the Armstrong number printed on screen.**

## Leave a Reply