## Java, J2EE & SOA Certification Training

- 31k Enrolled Learners
- Weekend
- Live Class

Finding Armstrong numbers using can a tricky task. Hence many interviews or exams feature this problem. In this article we will see how to implement Armstrong Number in C.

This article will focus on following pointers,

- Armstrong Number in C
- Program From An Armstrong Number
- Armstrong Number for N digits
- Armstrong Number using Functions

So let us start with the first topic of this article,

An Armstrong number of a three-digit number is a number in which the sum of the cube of the digits is equal to the number itself.

**Consider the example:**

153 is an Armstrong number

So, 1*1*1+5*5*5+3*3*3=1+125+27=153

Hence 153 is an Armstrong number.

Now let us continue with this article on Armstrong Number in C and take a look at how to implement a program for the same,

#include <stdio.h> int main() { int num, original, rem, sum = 0; //rem is remainder and original is the original number printf("Enter a three-digit Number: "); scanf("%d", &num); original = num; while(original != 0) { rem = original%10; sum =sum + rem*rem*rem; original=original/ 10; } if(sum == num) printf("%d is an Armstrong number.",num); else printf("%d is not an Armstrong number.",num); return 0; }

**OUTPUT:**

If it is an Armstrong number.

If it is not an Armstrong number:

In the program shown above, we first declare all the variables that we are going to use in the program. We use a num to hold the number entered by the user. The original variable is used to hold the original number. ‘rem’ is used to hold the remainder, that is required for calculation. Last we have the sum variable, which is assigned to zero.

We first accept a 3-digit number from the user and store it in num. We then assign this to the original. We do this so that we can do changes to the original in the code above and keep the user entered number safe, to use it to compare with the number we get after calculation.

Next, we have the while loop. This runs till the value of original is zero. Inside the while loop, there are three steps to perform. First, we get the last digit of the number and store it in rem. This is done by using mod operator on original. Mod operator gives the remainder when used. If we consider the example, 153% 10 will give us 3 and that will be stored in the rem variable.

In the next step, we add the cube of the rem variable to sum and assign it to the sum variable. In the last step, we divide the original by 10. This is done because we no longer require the last digit as it is already been used. Since it is an integer value the numbers after decimal points won’t be considered. This is repeated until the original is equal to zero. Then it exits while loop.

After this, we compare the sum to the num variable containing the user input number inside an if statement. If the sum is equal to num then it is an Armstrong number and the if part will be executed. Otherwise, the else part will be executed.

Let us take a look at the other ways of implementing this program,

**Armstrong Number for N digits**

We can write the same code to find Armstrong number for n digits number with one slight modification.

**CODE:**

#include <stdio.h> #include <math.h> int main() { int num, original, rem, sum = 0, n = 0 ; printf("Enter a Number: "); scanf("%d", &num); original = num; while (original != 0) { original /= 10; ++n; } original = num; while (original != 0) { rem = original%10; sum += pow(rem, n); original /= 10; } if(sum == num) printf("%d is an Armstrong number.", num); else printf("%d is not an Armstrong number.", num); return 0; }

**Output:**

In this program, there is a slight change as compared to the 3-digit Armstrong number detection program. The first change is that:

while (original != 0) { original /= 10; ++n; }

This is done to count the number of digits of the input. This is done until original is zero. N is incremented till original is zero. This is how we get the total digits.

After this, we again assign the number to original before it enters the second loop. Inside the second loop, we cubed the number and added it to sum in the 3-digit code.However, for n digits, we must use math.h function called pow to perform this exercise.

sum += pow(rem, n);

In this statement, we raise the remainder rem to the of n and add it to the sum variable. These are all the changes we need to make in this program.

Let us start with the final bit of this Armstrong number in C,

The same program can be executed using functions.

#include <stdio.h> #include <math.h> int armstrongNumberFinder(int n); int main() { int n, flag; printf("Enter a positive integer: "); scanf("%d", &n); flag = armstrongNumberFinder(n); if (flag == 1) printf("%d is an Armstrong number.", n); else printf("%d is not an Armstrong number.",n); return 0; } int armstrongNumberFinder(int num) { int original, rem, sum = 0, n = 0, flag; original = num; while(original != 0) { original /= 10; ++n; } original = num; while(original != 0) { rem = original%10; sum += pow(rem, n); original /= 10; } if(sum == num) flag = 1; else flag = 0; return flag; }

**Output:**

The output is exactly the same but, the structure of the program is changed. Here a function is called to calculate the Armstrong number and the result is returned to the main program. This increases the usability of the function.

**These are the various type of programs for finding Armstrong number.**

With this we come to the end of this blog on ‘Armstrong number In C’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can **enroll here** for live online training with 24/7 support and lifetime access.

*Got a question for us? Mention them in the comments section of this blog and we will get back to you.*