# C Program for Armstrong Number This blog post will go through how to determine whether a given number is an Armstrong number or not as a solution to this issue. This issue’s solution will be written in C. We’ll go through three possible strategies for fixing this issue. However, before we analyze the various solutions to this issue, it is important to understand what an Armstrong number is and who discovered it.
An Armstrong number is one where the sum of all its digits, raised to the number of digits, equals the number. Michael Armstrong found and gave his name to Armstrong’s number, commonly referred to as the narcissistic number.

## Armstrong Number

A number called the Armstrong number is the sum of its digits’ cubes. The Armstrong numbers, for illustration, are 0, 1, 153, 370, 371, and 407.
Before continuing with this article, we recommend that you should learn these C programming topics:

• For loop in C
• If statement in C
• Do while in C

## Logic Used for Armstrong

Consider the number 407; it has three digits, thus it can be written as:
(407) —> (444) + (000) + (777)
64 + 0 + 343
=> 407
407 is an Armstrong number because it is the sum of the cubes of its digits.

### Explanation with Example

Let input is equal to 153
variable values before for loop
num=153; check=153; Armstrong=0; result=0;
values of the variable in for loop are line by line

for i=1
Armstrong=3;
num=15;
Armstrong=333=27
result=0+27=27;
for loop condition check: num is not equal to zero loops will run again

for i=2
Armstrong=5;
num=1;
Armstrong=555=125
result=27+125=152;
for loop condition: num is not equal to zero loops will run again

for i=3
Armstrong=1;
num=0;
Armstrong=111=1;
result=152+1=153;
In for loop condition, the num is EQUAL TO ZERO therefore, the loop will run again

The loop will break and if else condition will be checked as or result=153 and check=153
if the condition will true program will show the output
153 is an Armstrong Number

## Armstrong Number Algorithm:

• Start
• Read an integer input number.
• Declare and initialize the variables current_digit , sum = 0 and num = number.
• Repeat Steps 5 to 7 Until num > 0.
• current_digit = (num % 10) %10 return the last digit of the current number
• sum = sum + (current_digit current_digit current_digit).
• The variable current_digit is multiplied three times because we check for a three-digit Armstrong number.
• num = num / 10

After processing the last digit, we need to remove it. /10 will give an integer such that the order of the number will be reduced and the next digit will become the last digit.

• Check if sum == number. Then Print “It is an Armstrong Number.” Else Print “It is not an Armstrong Number.”

## C Program Code to Check Armstrong Number

Method 1: In this method, we will take a three-digit number let’s say 371 and we will check if 371 is an Armstrong number or not.

```#include<stdio.h>
#include<math.h>

int isArmstrong(int number) {
int current_digit, sum = 0, num = number, number_of_digits;

while (num > 0) {
current_digit = num % 10;

sum = sum + pow(current_digit, 3);
num = num / 10;
}

if (sum == number) {
return 1;
} else {
return 0;
}
}

int main() {
int number = 371, isArmstrongNumber;

isArmstrongNumber = isArmstrong(number);

if (isArmstrongNumber == 1) {
printf("%d is an Armstrong Number.", number);
} else {
printf("%d is not an Armstrong Number.", number);
}

return 0;
}
```

Method 2: In this method, we will use different functions for checking if the given number is an Armstrong number or not.

```#include <stdio.h>
#include <math.h>

int countNumberOfDigits(int number, int number_of_digits) {

while (number > 0) {
number = number / 10;
number_of_digits++;
}

return number_of_digits;
}
int isArmstrong(int number) {
int current_digit, sum = 0, num = number, number_of_digits = 0;
number_of_digits = countNumberOfDigits(num, number_of_digits);
while (num > 0) {
current_digit = num % 10;
sum = sum + pow(current_digit, number_of_digits);
num = num / 10;
}
if (sum == number)
return 1;
else
return 0;
}

int main() {
int number, isArmstrongNumber;
scanf("%d", & number);
isArmstrongNumber = isArmstrong(number);
if (isArmstrongNumber == 1)
printf("%d is an Armstrong Number.", number);
else
printf("%d is not an Armstrong Number.", number);

return 0;
}
```

Time complexity: Time complexity will be O(X), where X = log10(N), and N is the number we are determining to be an Armstrong number because we are evaluating each digit of the number.

Space complexity: In this, we are not using any extra space, therefore the space complexity is O(1).

Tip: Many companies like Wipro, TCS, Accenture, and Infosys ask these types of questions in their hiring process to check the basic logic of the candidates.

Conclusion
We attempted to determine using C language whether the provided number is an Armstrong number or not. In this post. We trust this post has helped you better understand Armstrong’s number in this post has helped you understand Armstrong’s number in C better.

Other C Programs