# C Program To Check Armstrong Number

In this post, we will learn how to check an Armstrong Number using C Programming language.

A number is called an Armstrong Number if the sum of cubes of digits of the number is equal to the number itself.

For example: 371 is an Armstrong Number because (3)3 + (7)3 + (1)3 = 371.

So, without further ado, let’s begin the tutorial.

Contents

## C Program To Check Armstrong Number of 3 Digits

```// C Program To Check Armstrong Number
#include <stdio.h>
int main(){
int num, remainder, temp, result = 0;

printf("Enter a number: ");
scanf("%d", &num);

temp = num;
while (temp != 0){
remainder = temp % 10;
result += remainder * remainder * remainder;
temp = temp / 10;
}

if (result == num){
printf("%d is an Armstrong Number.", num);
}
else {
printf("%d is not an Armstrong Number.", num);
}
return 0;
}
```

Output 1

``````Enter an number: 370
370 is an Armstrong Number.
``````

Output 2

``````Enter an number: 147
147 is not an Armstrong Number.
``````

## How Does This Program Work ?

``````    int num, remainder, temp, result = 0;
``````

In this program, we have declared four int data type variables num, remainder, temp and result. The result variable is assigned a value of 0.

``````    // Asking for Input
printf("Enter a number: ");
scanf("%d", &num);
``````

Then, the user is asked to enter a number. The value will be stored in num named variable.

``````    temp = num;
while (temp != 0){
remainder = temp % 10;
result += remainder * remainder * remainder;
temp = temp / 10;
}
``````

Now, temp variable is assigned the value of num. Then, Run the loop until temp = 0. We extract the digit by dividing the number by 10.

Cube of the digit is find out using remainder * remainder * remainder.

Last digit of the number is dropped out by dividing the number by 10.

``````    if (result == num){
printf("%d is an Armstrong Number.", num);
}
else {
printf("%d is not an Armstrong Number.", num);
}
``````

Finally, the program checks whether the sum of the cube of digits is equal to the original number or not.

If yes, then the number is an Armstrong Number otherwise it’s not an Armstrong Number.

Some of the used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

scanf() – scanf() function is used to take input from the user.

printf() – printf() function is used to display and print the string under the quotation to the screen.

while Loop – In the while loop, the statements inside the body of the while loop keeps executing until it is evaluated to False.

% – It is known as Modulus Operator and provides remainder after division.

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True.

// – Used for Commenting in C.

## C Program To Check Armstrong Number of n Digits

```// C Program To Check Armstrong Number
#include <math.h>
#include <stdio.h>

int main(){
int num, remainder, temp, result = 0, length = 0;

printf("Enter a number: ");
scanf("%d", &num);

// Counting No. of Digits in num
temp = num;
while (temp != 0){
length++;
temp = temp / 10;
}

temp = num;
while (temp != 0){
remainder = temp % 10;
result += floor(pow(remainder, length));
temp = temp / 10;
}

// Displays Output
if (result == num)
printf("%d is an Armstrong Number.", num);
else
printf("%d is not an Armstrong Number.", num);

return 0;
}
```

Output 1

``````Enter an number: 1634
1634 is an Armstrong Number.
``````

Output 2

``````Enter an number: 1801
1801 is not an Armstrong Number.
``````

pow() function is used to calculate the power raise to the base value.

## Conclusion

I hope after going through this post, you understand how to check Armstrong number using the C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.