Here's a C Program to generate Armstrong numbers up to a limit n using C concepts like For Loop, While Loop, IF statement and Modulus. This post also contains output of this program and an explanation of how the program generates Armstrong Numbers up to a limit n.

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 i, a, r, s, n ; clrscr() ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThe armstrong numbers are :\n\n") ; for(i = 0 ; i <= n ; i++) { a = i ; s = 0 ; while(a > 0) { r = a % 10 ; s = s + (r * r * r) ; a = a / 10 ; } if(i == s) printf("%d\t", i) ; } getch() ; }

**Output of above program -**

Enter the limit : 1000

The armstrong numbers are :

0 1 153 370 371 407

**Explanation of above program -**

The above program first asks the user to enter a limit

**n**and then using a for loop, it calculates all the Armstrong numbers that lie between 0 and n. To understand the working of this program let’s take an example –
Suppose the user enters 1000 as limit n. In this case, the
for loop will run for the value of loop variable i = 0 to i = 1000 (or 1001
times). Let’s understand the process
inside the for loop.

**Working of for loop –**

- The first line inside the loop is creating a copy of the
current number (loop variable i) that we’re checking to be an Armstrong number and storing its
value in the variable
**“a”**. Further operations are done on this copy. In this way the original value is not modified and we can easily do the comparison needed to check whether that number is an Armstrong number or not. - In the second line of the loop, we’re initializing the sum s to zero at the start of new iteration, every time for all values of i.
- Next, there is a while loop, the process inside this while loop is same as the process used to generate the reverse of a number. The only difference is in the second line of while loop where we are adding the cubes of every digit of that number. (Take a look at the Program to Reverse an Integer if you need/)
- After this while loop, the variable
**s**holds the sum of cubes of all digits of a number. - Now to check whether that number is Armstrong or not all we have to do is to check whether the value of i and s are equal. Here i holds the original number we’re checking to be Armstrong and s holds the sum of cubes of all digits of that number. If these two value i.e. i and s are equal then that number “i” is Armstrong number and we print its value otherwise we continue with the next value of i. Also refer to this - Program to Check Whether the Given Number is Armstrong or Not . (This post contains further explanation about checking whether a number is Armstrong or not).

how can i get a 4 digit armstrong number solution

can i get 4 digit armstrong number solution using for loop

You just have to select a higher limit between which a four digit Armstrong lies e.g. select 2000 as the limit and check the output.

************************************************************

Enter the limit of Armstrong number : 1000000000

Armstrong numbers are :

1

2

3

4

5

6

7

8

9

153

370

371

407

1634

8208

9474

54748

92727

93084

548834

1741725

4210818

9800817

9926315

24678050

24678051

88593477

146511208

472335975

534494836

912985153Press any key to continue . . .

mine output

How to print first n amstrong numbers?

This program prints first n Armstrong numbers. You just need to pass a limit up to which u need to print the numbers.

This program doesnt print armstrong number above 407 . Even when we give limit as 1000000

This program doesnt print armstrong number above 407 . Even when we give limit as 1000000

Which processor u got bro

Hi Bru,

Great post. Well though out. This piece reminds me when I was starting out C Program to Generate Armstrong Numbers up to Limit n after graduating from college.

int main()

{

int a=7, t=0;

t=--a+--a+a+++a;

printf("%d",t);

}

output: 20

I could not understand why it prints '20' ?

By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).

Regards,

Irene Hynes

The great service in this blog and the nice technology is visible in this blog. I am really very happy for the nice approach is visible in this blog and thank you very much for using the nice technology in this blog

Php course in chennai