# 3-Digit / N-Digit Armstrong Number Program in C Language

## Program Description:

Write a Program to check if the given number is Armstrong Number in C Language. We are going to check the 3-Digit Armstrong number program and also the N-Digit Armstrong number program as well. The program should accept a positive number from the user and display if the number is an Armstrong number or not.

## What is an Armstrong Number?

An Armstrong number of an integer such that the sum of the cubes of its digits is equal to the number itself. Which is a 3-digit Armstrong Number.

Let’s look at a couple of 3-digit Armstrong number examples

#### Example 1:

Number 371 is an Armstrong number

since 3^3 + 7^3 + 1^3 = 371

#### Example 2:

Number 153 is also an Armstrong number

since 1^3 + 5^3 + 3^3 = 153

The above numbers are Three digits Armstrong numbers.

## What is an N-Digit Armstrong Number?

A positive number of n digits is said to be an Armstrong number of order n, If the sum of the power of n of each digit in the number is equal to the original number itself.

If the number is qwer is Armstrong number, Then it should satisfy the below equation.

qwer = pow(q, n) + pow(w, n) + pow(e, n) + pow(r, n)

Here n is the number of digits in the number qwer which is 4

For Four digit Armstrong numbers, We need to calculate the digit ^ 4 for each digit.

Similarly, for Five digit Armstrong numbers, We need to calculate the digit ^ 5 for each digit.

Here is an example, The number 54748 is an Armstrong number, So ( n = 5 i.e number of digits in the 54748)

54748 = pow(5, 5) + pow(4, 5) + pow(7, 5) + pow(4, 5) + pow(8, 5)

54748 = 3125 + 1024 + 16807 + 1024 + 32768

54748 = 54748

As the powers of n of each digit in the number is equal to the number itself. So the number 54748 is an Armstrong number.

Another example, The number 1634 is a Four digit Armstrong number as 1634 = 1^4 + 6^4 + 3^4 + 4^4.

Here is the list of Armstrong numbers ( based on the number of digits).

## Program Pre-Requisites:

We are going to write two programs to calculate the Armstrong number.

1. Armstrong number Program for Three digits Number in C
2. Armstrong number Program for N-Digits number in C.

## Armstrong Number Program in C Language Algorithm [For 3 digit Number]:

In Nutshell, We need to get each digit of the number and then calculate the cubes of each digit. Then add all cubes which should be equal to the given number. If the cubes are equal to the given number then it’s an Armstrong number. Otherwise, it is not an Armstrong number.

Here is the Algorithm.

1. Take the input from the user and store it in variable n.
2. Check if the given number n is positive or negative. Display an error message if the number n is negative.
3. Let’s use an extra variable temp to hold the input number n. As we need 'n' to verify our result.
4. Start the Loop, The loop will continue as long as the variable temp is greater than Zero(0). – temp > 0.
5. At Each Iteration
• We are going to use the Modulus operator to get the last digit of the number, We can get the last digit of any number by performing the modulus with 10.
• For example, 8798 % 10 = 8
• Once we get the last digit, Calculate the cube of the digit. Then add to arm variable ( arm variable holds the Armstrong number so far). The arm variable is initialized with zero at the start. and We will add the Cubes of each digit to the arm variable.
• As we processed the last digit, We can remove the last digit of the number temp by dividing the number by using the number 10.
• For example, 8798 / 10 = 879
6. The above step-5 continues until temp > 0 becomes False.
• So at each iteration, we are going to get the last digit and then calculate the cube of that digit, Then Add the cube to the arm variable. Finally, We removed the last digit from the number. So once we pass through the all digits of the temp. The temp will become less than zero. And our loop will be terminated.
7. Check if the arm is equal to the original number n.
• If both arm number and variable n are equal, Then the given number n is an Armstrong number.
• If numbers arm and n are not equal, Then n is not an Armstrong number.

## Check or Detect an Armstrong Number program in C Language [For 3-Digit Number]:

Here is the Armstrong number program implementation in C language. We are using the while loop to iterate over the number ( temp).

The program also performs the error check and displays an error message on Invalid inputs like negative numbers.

## Armstrong Number Program in C Program Output:

Compile and run the program. We are using the GCC compiler under the Linux Operating system to Run and compile the program.

gcc armstrong.c

Above command, generate the executable file named a.out. Run the program using the ./a.out command.

Test Case 1: Positive Numbers

As you can see from the above output, We are getting the expected results.

Let’s also verify the output for the negative numbers.

The program is displaying the error message when the user enters a Negative number – Error: Please enter positive number

## Armstrong Number Program in C for N-Digits Number Algorithm:

For the three-digit Armstrong number, We calculated the Cubes of each digit. Similarly, for N-Digits Armstrong numbers we need to calculate the power of nDigits i.e pow(digit, nDigits).

Here is the algorithm to check the N-Digit Armstrong number

1. Take the input from the user and store it in a variable n. Display error if the n is a Negative number.
2. First of all, We need to Calculate the Number of digits in the number. Which is used to calculate the power. We used a function countDigits and passed the input number n to calculate the number of digits and countDigits function returns the number of digits in the number( n).
3. Then we need to go through each digit of the given number n using the Loop.
• At Each Iteration,
• Get the digit from the number i.e n . rem = temp %10;
• Calculate the power of rem with nDigits(Number of digits in n). We are using the Power function from the math.h header file – pow(rem, nDigits);. Update the arm variable.
• Remove the digit using the temp/10 operation.
4. Once the above loop is terminated, If the arm variable is equal to n, Then given the number n is N-digit Armstrong Number.

## N-Digit Armstrong Number Program in C Program:

As we have used the pow function, We need to include the math.h header file.

### Program Output:

The pow function available as part of the libm.so. So We need to pass the library function to the GCC using the -lm option.

Here is the command to compile the C Program which using the functions from the math.h header file.

gcc armstrong-n-digits.c -lm

The above command generates the output file named a.out. Run the executable.

Let’s give the 4 digit Armstrong numbers to the program.

As expected, The program is able to detect the 4-digit Armstrong numbers.

Let’s try 5-digit Armstrong number.

The program is working as expected. If you plan to use the very large numbers, Then make sure to use the appropriate datatypes like (long int, long long int, etc).

## N Digit Armstron Number without using Inbuilt Functions:

In the above program, We used the in-built pow function to calculate the power of the number. Let’s use our custom pow function instead of the pow library function.

We are using the Power of Number Program in C to calculate the power instead of pow built-in function.

Please note that, We haven’t used the math.h headerfile, As we no longer using the pow function.

### Program Output:

Compile and run the program.

gcc armstrong-n-digits-without-pow.c

📢 Note that We haven’t passed -lm Compilation Option to GCC. As we are not using the pow function.

Here is the program output form different digit numbers like 3-digits, 4-digits, 5-digits, and 6-digits Armstrong numbers.

As we can see from the above output, The program is able to calculate the N-Digit Armstrong numbers correctly.

## Related Programs: Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 9 Responses

1. […] Armstrong Number Program in C Language – SillyCodes […]

2. […] Armstrong Number Program in C Language – SillyCodes […]

3. […] Armstrong Number Program in C Language – SillyCodes […]

4. […] Armstrong Number Program in C Language – SillyCodes […]

5. […] Armstrong Number Program in C Language – SillyCodes […]

6. […] C Program to Check Armstrong Number […]

7. […] our previous article, We have looked at the 3-Digit and N-digit Armstrong number program. In today’s article, We are going to extend that program and will write a program to generate […]

8. […] Program Check 3-digit / N-digit Armstrong Numbers in C language […]

9. […] C Program to Check 3-Digit and N-Digit Armstrong Number […]