# C Program to check a Number is Armstrong or not

## Program to check a number is Armstrong or not

In this program, we will find the number is Armstrong or not where the number should be entered by the user, we will look at different ways to write Armstrong number in C.

Most websites have given incorrect definitions/codes for Armstrong of a number.

### Very Important

For Armstrong number in C on most websites, Armstrong number definition is given wrong. They all assume any number where the sum of the cubes of its digits is equal to the number itself is Armstrong Number.

However, Armstrong number is any number following the given rule –

• abcd… = an + bn + cn + dn + …
• Where n is the order(length/digits in number)

Also, Check Armstrong Number in a given Range in C

## C Code:-

Run
```#include <stdio.h>
#include <math.h>
int order(int x)
{
int len = 0;
while (x)
{
len++;
x = x/10;
}
return len;
}
int getArmstrongSum(int num, int order){

if(num == 0)
return 0;

int digit = num % 10;

return pow(digit, order) + getArmstrongSum(num/10, order);
}

// Driver Code
int main ()
{
int num, len;
num=1634;
printf("The number is:%d\n",num);

// function to get order(length)
len = order(num);

// check if Armstrong
if (num == getArmstrongSum(num, len))
printf("%d is Armstrong", num);
else
printf("%d is Not Armstrong", num);

}```

## Output:-

`The number is: 16341634 is Armstrong`

## Method 2

This method uses recursion in C

### Code

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

// Armstrong number is any number following the given rule
// abcd... = a^n + b^n + c^n + d^n + ...
// Where n is the order(length/digits in number)

// Example = 153 (order/length = 3)
// 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

// Example = 1634 (order/length = 4)
// 1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634

// number of digits in a number is order
int order(int x)
{
int len = 0;
while (x)
{
len++;
x = x/10;
}
return len;
}

int armstrong(int num, int len){

int sum = 0, temp, digit;
temp = num;

// loop to extract digit, find power & add to sum
while(temp != 0)
{
// extract digit
digit = temp % 10;

sum = sum + pow(digit,len);
temp /= 10;
};

return num == sum;
}

// Driver Code
int main ()
{
//variables initialization
int num = 1634, len;

// function to get order(length)
len = order(num);

// check if Armstrong
if (armstrong(num, len))
printf("%d is armstrong\n",num);
else
printf("%d is not armstrong\n",num);

return 0;
}```

#### Output

```The number is: 1634
1634 is Armstrong```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

### 5 comments on “C Program to check a Number is Armstrong or not”

• NAINARU SIVARAMAKRISHNA

#include
int main()
{
int num=153,sum=0,rem=0;
int val=num;
while(num>0){
rem=num%10;
rem=pow(rem,3);
sum=sum+rem;
num/=10;
}
if(val==sum){
printf(“armstrong number”);
}
else{
printf(“not armstrong number”);
}
}

#include
#include

int main()
{
int n,sum=0,rev=0;
scanf(“%d”,&n);
int temp=n;
int x=n;
int count=0;
while(n!=0){
int r=n%10;
rev=rev*10+r;
n=n/10;
count++;
}
printf(“%d”,count);

int y=0;
int z=0;
while(temp!=0){

z=temp%10;
y=y+(pow(z, count));
//printf(“%d\n”,y);
temp=temp/10;
}
printf(“\n%d”,y);
if(x==y){
printf(“\nnum is a Armstrong”);
}
else{
printf(“\nnum is not a Armstrong”);
}

return 0;
}

• Arnab

#include
#include
int main()
{
int num,i=0,num1,num2,sum=0,rem;
printf(“Enter Number\n”);
scanf(“%d”,&num);
if(num>0 && num<10)
printf("Armstrong Number");
else
{
num1=num;
while(num1!=0)
{
num1=num1/10;
i++;
}
num2=num;
while(num2!=0)
{
rem=num2%10;
num2=num2/10;
sum=sum+pow(rem,i);
}
if(sum==num)
printf("Armstrong Number");
else
printf("Not Armstrong Number");
}
return 0;
}

• Sujay

simple version
#include
int main() {
int num, originalNum, remainder, result = 0;
printf(“Enter a three-digit integer: “);
scanf(“%d”, &num);
originalNum = num;

while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;

result += remainder * remainder * remainder;

// removing last digit from the orignal number
originalNum /= 10;
}

if (result == num)
printf(“%d is an Armstrong number.”, num);
else
printf(“%d is not an Armstrong number.”, num);

return 0;
}