# Armstrong number in C using function

In this post, you will learn how to write a **C program** to check whether a given number is **Armstrong** or not using a function.

A number is called an **Armstrong** number if the sum of its cubes of digits is equal to the number itself. It is also known as the **Narcissistic** number. To lay it out simply, assuming we have a 3-digit number, each of the digits is raised to the power of three and added to get a number. If the number obtained equals the original number, we call that an Armstrong number.

In number theory, we can say that an Armstrong number in a given number base b is a number that is the sum of its own digits, each raised to the power of the number of digits.

## Formula to check Armstrong number

A positive number of **n** digits is called an Armstrong number of order **n**.

**abcde.... = a**^{n} + b^{n} + c^{n} + d^{n} + e^{n} +

The following example demonstrates why **371** is an Armstrong number.

```
371 = (3*3*3)+(7*7*7)+(1*1*1)
where:
(3*3*3)=27
(7*7*7)=343
(1*1*1)=1
So:
27+343+1 = 371
```

## C program to check Armstrong number using function

In the given **C** program, we take input from the user and store it in a variable called **num**. Next, we define a custom function **armstrong()** and pass the entered value in argument. Next, we split all the digits of the **num** and calculate the cube value of the digits. Then, we add all cube values together and save the output to the variable **sum**. If the **sum** equals the **num**, then the **num** is an Armstrong number; if the **sum** is not equal to **num**, then the **num** is not an Armstrong number.

We call this a custom function in our main function. If the custom function returns 0, then the entered number is an Armstrong number.

```
// Armstrong Number in C Using Function
#include<stdio.h>
int armstrong(int orgnum){
int remainder = 0;
int sum = 0;
int power = 0;
int num = orgnum;
while (num != 0){
// remainder contains the last digit
remainder = num % 10;
power = remainder * remainder * remainder;
sum = sum + power;
num = num / 10;
}
if (sum == orgnum)
return 0;
else
return 1;
}
int main(){
int num;
// getting input from user
// and store in a variable
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 of the above code:**

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

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

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

## C program to check Armstrong number of n digits using function

In the given **C** program, we check the armstrong number of **n** digits using a custom function.

```
// Armstrong Number in C Using Function
#include <stdio.h>
int armstrong(int orgnum){
int remainder = 0;
int sum = 0;
int num,n = 0;
float result = 0.0;
num = orgnum;
for (num = orgnum; num != 0; ++n) {
num /= 10;
}
for (num = orgnum; num != 0; num /= 10) {
remainder = num % 10;
// store the sum of the power
// of individual digits in result
result += pow(remainder, n);
}
// if num is equal to result,
// the number is an Armstrong number
if ((int)result == orgnum)
return 0;
else
return 1;
}
int main(){
int num;
// getting input from user
// and store in a variable
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 of the above code:**

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

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

### Related Articles

**C program to check whether a number is positive or negative**

Priority queue using array in C

Simple calculator program in C

C program for simple interest

Bit stuffing program in C

Prime factors of a number in c

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to find greatest of three numbers

C program for addition of two numbers

C program to calculate compound interest

C program to find the ASCII value of a character

C program to convert Decimal to Octal

C program to convert decimal to binary

Write a C program to calculate Simple Interest

C program to check whether a number is even or odd

Priority queue using array in C

Simple calculator program in C

C program for simple interest

Bit stuffing program in C

Prime factors of a number in c

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to find greatest of three numbers

C program for addition of two numbers

C program to calculate compound interest

C program to find the ASCII value of a character

C program to convert Decimal to Octal

C program to convert decimal to binary

Write a C program to calculate Simple Interest

C program to check whether a number is even or odd