# C Program to Check Armstrong Number

The program to check an Armstrong number is written using Dev-C++ compiler, but you can use any standard C compiler to compile the program.

This program is intended for beginner C programmers.To help you understand and learn the program, following sections will be helpful – problem definition, flowchart, problem code and a verified output.

Numbers have many interesting properties. Given a number, we want to check if the number is an Armstrong number or not.

### Problem Definition

What is an Armstrong Number?

Suppose there is a number N, this number has n digits, if

• We take each digit of the number N separately
• Compute nth power of each digit, and
• Take the sum, S of all the power obtained in step 2.
• If the sum is equal to original number, N;
• Then the given number N is an Armstrong number.

For Example

$371 = 3^3 + 7^3 + 1^3$

$27 + 343 + 1$

$371$

The number N in above example is 371 and number of digits n = 3. So we raise each of the digits of 371 to power to 3. Then sum of all power  is equal to original number if the number is an Armstrong number.

How do we process the Input number N?

Step 1 – Get the number N

Step 2 – Split each digit of the number

Step 3 – Raise each digit to the power of 3 and take sum of all the powers.

Step 4 – Repeat step 1 to 3 if number N not equal to 0.

Step 5 – Check if Sum == N after Step 4, N = = 0.

Step 6 – Print the Output.

### Program Code

``````#include <stdio.h>
#include <conio.h>
main()
{
int n,i,n1,rem,num =0;
printf("Enter a positive integer:");
scanf("%d",&n);
//Get the sum of nth power of each digit
n1 =n;
while(n1 != 0)
{
rem = n1 % 10;
num += rem * rem * rem;
n1/=10;
}
//Check if the number is an Armstrong or Not
if(num == n)
{
//Print the result
for(i =0;i<30;i++)
{
printf("_");
}
printf("\n\n");
printf(" %d is an Amstrong number\n",n);
for(i =0;i<30;i++)
{
printf("_");
}
printf("\n\n");
}
else
{
for(i =0;i<30;i++)
{
printf("_");
printf("\n\n");
}
printf("%d is not an Armstrong number",n);
for(i =0;i<30;i++)
{
printf("_");
printf("\n\n");
}
}
getch();
return 0;
}``````

### Output

Here is the output of the program for an input integer 371.