# Armstrong Number in C Using Function

In this post, we will learn how to check Armstrong number in C using function.

As we know, Armstrong Number is a number that is equal to the sum of cubes of its digits. For example: 371 is an Armstrong number because (3)3 + (7)3 + (1)3 = 27 + 343 + 1 = 371. So, 371 is an Armstrong number of order 3.

Similarly, 8208 is an armstrong number of order 4.

We will declare a custom function which will check whether the number is an armstrong number or not.

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

Contents

## Armstrong Number in C Using Function

```// Armstrong Number in C Using Function
#include <stdio.h>
int armstrong(int num){
int lastdigit = 0;
int sum = 0;
int power = 0;

int n = num;
while (n != 0){
lastdigit = n % 10;
power = lastdigit * lastdigit * lastdigit;
sum = sum + power;

n = n / 10;
}
if (sum == num)
return 0;
else
return 1;
}

int main(){
int num;

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

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

Output 1

``````Enter a number: 371
371 is an Armstrong Number.
``````

Output 2

``````Enter a number: 144
144 is not an Armstrong Number.
``````

## How Does This Program Work ?

``````int armstrong(int num){
int lastdigit = 0;
int sum = 0;
int power = 0;

int n = num;
while (n != 0){
lastdigit = n % 10;
power = lastdigit * lastdigit * lastdigit;
sum = sum + power;

n = n / 10;
}
if (sum == num)
return 0;
else
return 1;
}
``````

In this program, we have declared a custom function named armstrong which will check whether the entered number is an Armstrong number or not.

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

Then, we call this custom function in our main function which will check for the entered number. If the custom function returns 0, then the entered number is an Armstrong number.

Otherwise, the entered number is not an Armstrong number.

## Armstrong Number in C of Order N Using Function

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

int armstrong(int num, int order)
{

int lastDigit = 0;
int sum = 0;
int power = 0;

int n = num;

while(n!=0) {

// find last digit
lastDigit = n % 10;

// find power of digit
power = pow(lastDigit, order);

// add power value into sum
sum += power;

// remove last digit
n /= 10;
}

if(sum == num)
return 0;
else
return 1;
}

int main()
{
int num, order;

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

printf("Enter Order: ");
scanf("%d",&order);

if(armstrong(num, order) == 0)
printf("%d is an Armstrong number of order %d.\n", num, order);
else
printf("%d is not an Armstrong number of order %d.", num,order);
return 0;
}
```

Output 1

``````Enter number: 371
Enter Order: 3
371 is an Armstrong number of order 3.``````

Output 2

``````Enter number: 7001
Enter Order: 4
7001 is not an Armstrong number of order 4.
``````

## Conclusion

I hope after going through this post, you understand how to check armstrong number in C Using Function.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query.