# Armstrong Number in C

An Armstrong Number is defined as the sum of the nth power of each digit to an n-digit number that is equal to that number. Confused! Don’t worry. In this article, we will discuss everything about the Armstrong Number with the help of examples, and later in the article, we will also discuss how to check whether the given number is Armstrong Number or not using C programming.

In the previous articles, we have discussed:

This article will discuss different methods to check the Armstrong Number in C Programming.

But before that, let’s discuss what is an Armstrong Number.

**Armstrong Number (or Narcissistic Number)** is a special type of positive integer that is equal to the sum of its own digit raised to the power of the number of digits in that number. i.e.,

Any integer ‘abcd’ is known as an Armstrong number if,

**abcd = a ^{4} + b^{4} + c^{4} + d^{4}**

**Example:**

- 153 = 1
^{3}+ 5^{3}+ 3^{3} - 1634 = 1
^{4}+ 6^{4}+ 3^{4}+ 4^{4}

**Note:** A number with n digits is called Armstrong Number of order n, where the sum of each digit powered by n is the same as the number.

**a _{1}a_{2}a_{3}….a_{n} = (a_{1})^{n} + (a_{2})^{n} + (a_{3})^{n} + …. + (a_{n})^{n}**

Until now, you have a better understanding of what is Armstrong’s number.

## Crack the code of C/C++ with our comprehensive guide; find details on top colleges, programmes and online courses.

**How to Find an Armstrong Number Using C Programming?**

To check whether a given number is an Armstrong number or not, we will discuss two different methods:

**Armstrong Number in C using Function**

#include <stdio.h>#include <math.h>#include <stdbool.h>
bool isArmstrong(int num) { int originalNum = num; int sum = 0; int digitCount = 0;
// Count the number of digits in the input number while (num != 0) { num /= 10; digitCount++; }
// Reset num to the original value for further processing num = originalNum;
// Calculate the sum of cubes of digits using mathematical formula while (num != 0) { int digit = num % 10; sum += pow(digit, digitCount); num /= 10; }
// Check if the sum is equal to the original number return (sum == originalNum);}
int main() { int num; printf("Enter a number: "); scanf("%d", &num);
if (isArmstrong(num)) { printf("%d is an Armstrong number.\n", num); } else { printf("%d is not an Armstrong number.\n", num); }
return 0;}

**Output**

**Explanation**

- Define a function ‘isArmstrong’ that takes an integer as an argument.
- Inside the function, count the number of digits in the input number.
- Initialize a variable (named it as ‘sum’) to store the sum of the power (nth power) of the digits.
- Calculate the ‘sum’ with the original input number using the mathematical formulas.
- If the ‘sum’ is equal to the input number, return TRUE (TRUE indicate that the number is an Armstrong number); otherwise, return FALSE.

**Armstrong Number in C using Loop**

#include <stdio.h>#include <math.h>
int main() { int num; printf("Enter a number: "); scanf("%d", &num);
int originalNum = num; int sum = 0; int digitCount = 0;
// Count the number of digits in the input number while (num != 0) { num /= 10; digitCount++; }
// Reset num to the original value for further processing num = originalNum;
// Calculate the sum of cubes of digits using a loop while (num != 0) { int digit = num % 10; sum += pow(digit, digitCount); num /= 10; }
// Check if the sum is equal to the original number if (sum == originalNum) { printf("%d is an Armstrong number.\n", originalNum); } else { printf("%d is not an Armstrong number.\n", originalNum); }
return 0;}

**Output**

**Explanation**

- Input the number whose Armstrong property needs to be checked.
- Count the number of digits in the input number.
- Initialize a variable (let’s call it “sum”) to store the sum of the cubes of the digits.
- Using a loop, extract each digit from the input number, cube it, and add it to the “sum” variable.
- After the loop finishes, compare the “sum” with the original input number.
- If the “sum” is equal to the input number, then the number is an Armstrong number; otherwise, it is not.

**Conclusion**

An Armstrong Number is defined as the sum of the nth power of each digit to an n-digit number that is equal to that number. In this article, we have discussed different methods to check Armstrong numbers in C.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

## FAQs

**What is an Armstrong number?**

An Armstrong number is a number that is equal to the sum of the cubes of its own digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

**Can you check for Armstrong number for negative or decimal numbers?**

Armstrong number is only defined for positive integers, So, you can not check for negative or decimal numbers.

**How to check Armstrong number for a large number?**

The logic is the same as with small numbers, but you need to use a larger data type like long or Big Integer to hold the large number and result of the cubing and summing.

**Is it necessary to check for the length of the input number?**

Yes, it is necessary to check for the length of the input number, because the length of the input number is used to calculate the sum of cubes of the digits of the number.

**About the Author**

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio