# Armstrong Number in C Programming

## Introduction

In this post, I am going to write a program to check whether a number is Armstrong or not in c programming. Before writing a c program, we must know what is Armstrong number?

### What is Armstrong number?

If the sum of cubes of all its digits of the number is equal to the same number, then that number is called as Armstrong number. For example, 153 is an Armstrong number. How? 13 +53 +33 =1+125+27 = 153.

Now, let’s see the program.

### Armstrong Number in C using while loop

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,n1,rem,sum=0;
printf("Enter a number\n");
scanf("%d",&n);
n1=n;
while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(n1==sum)
{
printf("%d is an Armstrong number",n1);
}
else
printf("%d is not an Armstrong number",n1);
return 0;
}
``````

Now, see the output of this program.

#### Output

``````Enter a number
153
153 is an Armstrong number``````

As you can see the above program with its output. We are reading one number from the user and then displaying the message whether that number is an Armstrong or not. What exactly we are doing in the program. See the following steps.

1. We are finding the remainder.
2. After this, we are taking the cube of that remainder and adding that cube in the variable sum.
3. Now, we will go for the next remainder. For this, we are dividing the number by 10 each time. So, the number is getting shorter by 1 digits every time.
4. We will repeat above three steps until the number is greater than or equal to zero.
5. In this step, we will compare the original number and the value of the variable sum. If these two are equal then the number is an Armstrong number otherwise not.

We can write the same program using for loop also. We have just some changes in the above program.

### Armstrong Number in C using for loop

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,rem,sum=0,i;
printf("Enter a number\n");
scanf("%d",&n);
for(i=n;i>0;i=i/10)
{
rem=i%10;
sum=sum+rem*rem*rem;
}
if(n==sum)
{
printf("%d is an Armstrong number",n);
}
else
printf("%d is not an Armstrong number",n);
return 0;
}``````

We can also write the same program using function also. Now see the following program.

### Armstrong Number in C using function

Here, we are sending the value which is read from the user to the function and the sum of the cubes of the digits of that number is returned by the same function.

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int armstrong(int);
int n,n1;
printf("Enter a number\n");
scanf("%d",&n);
n1=armstrong(n);
n1=n;

if(n==n1)
{
printf("%d is an Armstrong number",n);
}
else
printf("%d is not an Armstrong number",n);
return 0;
}
int armstrong(int n)
{
int rem,sum=0;
while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
return (sum);
}``````

### Armstrong Number in C Between Two Intervals

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,n1,n2,rem,i,sum=0,counter=0;
printf("Enter two numbers as a given range!\n");
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
n=i;
sum=0;
while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(i==sum)
{
printf("%d ",i);
counter++;
}
}
printf("\nTotal Armstrong Numbers in a given range: %d",counter);
return 0;
}
``````

#### Output

``````Enter two numbers as a given range!
1 1000
1 153 370 371 407
Total Armstrong Numbers in a given range: 5``````

I hope you have understood all these programs. If you have any doubt or any feedback, then please feel free to contact me.

## FAQ

Armstrong Number in C Between 1 to 1000

In this program, we have to write a c program to find Armstrong number between 1 to 1000.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,rem,i,sum=0;
for(i=1;i<=1000;i++)
{
n=i;
sum=0;
while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(i==sum)
{
printf(“%d “,i);
}
}
return 0;
}