# Find Armstrong Number C Program

## C Program To Find Armstrong Number

Here’s a C Program To Find Armstrong Numbers. Learn How To Check if a Number is an Armstrong Number or not in C Programming Language. It is important that we should know **How A For Loop Works** before getting further with the C Program Code.

#### What is an Armstrong Number?

If the Sum of Cubes of Individual Digits of an Integer is equivalent to the Integer, then it can be regarded as an Armstrong Number. Armstrong Number is similar to a **Narcissistic Number**.

#### Example

**153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3) 371 = (3 * 3 * 3) + (7 * 7 * 7) + (1 * 1 * 1)**

We have written 3 different variants to Check Armstrong Integers in C Programming Language which includes While Loop, For Loop and Functions.

#### How does this program work?

- The original number is copied into a temporary variable
- The last digit of the original number is extracted and cubed and added in the sum variable.
- The last digit is extracted till the original number is not equal to zero.
- After extraction and cubing, the sum variable is compared with the temporary variable.
- If sum == temp, Number is an Armstrong Number, else it is not an Armstrong Integer.

#### Method 1: Find Armstrong Number in C Programming using **While Loop**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<stdio.h> int main() { int num1, temp, sum = 0, rem; printf("\nEnter a Number:\t"); scanf("%d", &num1); temp = num1; while(num1 != 0) { rem = num1%10; sum = sum + (rem*rem*rem); num1 = num1/10; } if(sum == temp) { printf("\n%d is an Armstrong Number\n", temp); } else { printf("\n%d is not an Armstrong Number\n", temp); } return 0; } |

#### Method 2: C Program To Check** Armstrong Number using**** For Loop**

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> int main() { int num1, temp, sum = 0, rem; printf("\nEnter a Number:\t"); scanf("%d", &num1); for(temp = num1; num1 != 0;) { rem = num1%10; sum = sum + (rem*rem*rem); num1 = num1/10; } if(sum == temp) { printf("\n%d is an Armstrong Number\n", temp); } else { printf("\n%d is not an Armstrong Number\n", temp); } return 0; } |

#### Method 3: **Find Armstrong Number in C Programming using ****Function**

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 | #include<stdio.h> int armstrong(int x); int main() { int num1, res; printf("\nEnter a Number:\t"); scanf("%d", &num1); res = armstrong(num1); if(num1 == res) { printf("\n%d is an Armstrong Number\n", num1); } else { printf("\n%d is not an Armstrong Number\n", num1); } return 0; } int armstrong(int x) { int sum = 0,rem; while(x != 0) { rem = x%10; sum = sum + (rem*rem*rem); x = x/10; } return sum; } |

#### Output

In case you get any Compilation Errors or any doubts in this C Program To Find Armstrong Number, let us know about it in the Comment Section below.

Thanks! It works perfect!

Glad that you’re program worked fine.

If input is 10 output is ten Armstrong numbers

Can we get that?

Wow. Thanks for so many armstrong number programs in different ways. This has made my work more easier.

I am getting confused in void main and int main. The armstrong number programs that are here jave int main() declarations. Is it compulsory to use them?

Hi Pankaj! int main() is primarily used when you want to fetch arguments using Command Line. Otherwise, void main() will work. This Armstrong Number C Programs above can be made to work through command line by adding two arguments in the main function. (int argc, int *argv).

In this Armstrong Integer Program, I am getting confused with the modulus operator. Can you help me understand the use of Modulus Operator?

This is just awesome. So many ways to write armstrong number program in c! It helped me to clear my concepts functions and also how to convert for loop into while loop.

So many methods have made my basics clear about Armstrong Numbers and C programming looping system. I have a doubt. When we enter three numbers, the program shows correct output, but if the number consists of more than 4 digits, it shows a wrong output.

Hi Piyu. Armstrong Number program calculates only upto 3 digits. If you want to find armstrong number more than 3 digits, you should check its big brother Narcissistic Number. The Narcissistic Numbet C Program link is given in the above program.

I tried using float num instead of taking an integer data type. It is showing error. Please help.

Hi Rajendra. Well, the float variables cannot be used an an operand with Modulus Operator. That is why you would be getting errors. Please check which datatypes work with Modulus Operator and then select a suitable datatype.

You have executed this Armstrong Number C Program in Linux. Is it compatible if I execute it on Windows platform?

Yes. This Armstrong Integer code is written in Linux without any IDE. There should not be any problem to run this code on Windows platform.

Why not use Recursion to calculate Armstrong Numbers,

Yes. You may use Recursive function also to find solution to Armstrong Number problem.

I think the condition num!=0 can also be changed to num>0 or num>=1 in this code.

while(num1 != 0)

{

rem = num1%10;

sum = sum + (rem*rem*rem);

num1 = num1/10;

}

Yes. Your code will work perfectly fine in finding armstrong integers.

Why do we need to copy the armstrong number variable num1 into temp? I also see that the sum variable os compared to temp. Why can’t we compare it to num1 variable?

How do I cube the remainder variable? Is there a built in function for it or do we have to make a user defined function?

for(temp = num1; num1 != 0;)

{

rem = num1%10;

sum = sum + cube(rem);

num1 = num1/10;

}

int cube(int a, int b, int c)

{

int r = a*b*c;

return r;

}

The narcissistic number code is much better than just an Armstrong number code. The armstrong number code can find the cubes only upto 3 digits and what after that? On the other hand, the narcissistic number is an advanced version of the armstrong number which can calculate cubes of numbers having more than 3 digits.

Can we get possible number of Armstrong numbers within a range? Ex,if input is 10 ,10 Armstrong numbers should be output?

Yes. You may have to add a For loop and run it till the ending limit of your range.

Gladly work your program