## 3 Ways To Find Armstrong Number in C Programming

Let us understand what is an ArmstrongInteger Series and let us learn how we can implement Armstrong Number in C programming language.

#### What is an Armstrong Number Series?

An Armstrong number is the sum of cubes of the individual digits of the number. If the sum of the digits is equal to the number, then it is said to be an Armstrong Integer.

An Armstrong integer is similar to a **narcissistic number**. However, there’s a difference between an Armstrong number and narcissistic number.

An Armstrong integer is essentially a 3 digit number whereas a narcissistic number is a 4 or more than 4 digit number. It is also usually regarded as **4 digit Armstrong Number**.

#### Armstrong Number Formula

#### Example

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

Armstrong numbers are essentially **3 digit integers**. However, if you intend to calculate 4 digit armstrong number in C programming, you need to check the **narcissistic number c program**.

#### Algorithm For Armstrong Number in C Programming

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Step 1: Start Step 2: Input <strong>num1</strong> from the user, assign temp = num1 and sum = 0 Step 3: while(num1 > 0) do rem = num1 % 10 sum = sum + (rem * rem * rem) num1 = num1 / 10 Else go to Step 5 Step 4: Go to Step 3 Step 5: If (temp == sum) Then Print Armstrong Number Else Print Not an Armstrong Number EndIf Step 6: Stop |

#### Armstrong Series Logic Explanation

- The input value is copied into a
**temporary**variable. - The last digit of the input value is extracted, cubed and added to the
**sum**variable. - The last digit is extracted till the input value is not equal to
**zero**. - After the process is completed and the remainder is zero, the
**sum**variable is compared with the**temporary**variable. - If the sum and temporary variables are equivalent, the input value is an Armstrong Number, else it is not an Armstrong Integer.

**Note:** This C program for Armstrong Numbers is compiled with GNU GCC compiler on Linux Ubuntu 14.04 operating system. However, these codes are compatible with all other operating systems.

**Must Read: C Program To Check Palindrome Numbers**

#### Method 1: C Program To Find Armstrong Numbers 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 Integerr\n", temp); } else { printf("\n%d is not an Armstrong Integer\n", temp); } return 0; } |

**Must Read: Tribonacci Series C Program**

#### Method 2: Generate Armstrong Numbers in C Programming 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 Integer\n", temp); } else { printf("\n%d is not an Armstrong Integer\n", temp); } return 0; } |

**Must Read: C Program To Check if a Number is Magic Number or Not**

#### Method 3: C Program To Check** Armstrong Number 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 Integer\n", num1); } else { printf("\n%d is not an Armstrong Integer\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; } |

**Must Read: C Program To Check Strong Numbers**

#### Output

Let’s discuss more on Armstrong number in C programming in the comment section below if you have any compilation errors and any doubts about the same.

For more information on Armstrong series, check NCTM.

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

Brother which compiler and text editor do you use please tell me.

Hi Rahul. It is GCC Compiler(Linux Ubuntu’s default C Compiler) with the gEdit editor (Linux Ubuntu’s default text editor).

How do I do this using a recursive function?

So, as far as I understood is that Armstrong number is a 3 digit number and the 4 digit armstrong number is a narcissistic number. Please correct me if I am wrong!

Hi tushar

Can u plz explain why the sum in the if statement is not equalized to num1 in method 2 using for loop?Rather it is equalized to temp. Plz explain

When you perform: num1 = num1/10, the original num1 gets modified. So, you cannot compare the sum variable with the modified value. Hence, we are using temp variable to check whether the original num1 is equal to sum or not.

Thank you! Just a silly mistake?

and what’s that? 😀

Didn’t watched that num1 will change after every iteration

Okay! No problem! Bade bade deshon mein aisi choti choti galtiyaan hoti rehti hai! 😉

Thank you so much!