Program to Generate Armstrong numbers upto n in C Language


Armstrong Numbers upto n in C Program Description:

In 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 the Armstrong numbers upto n in C language. n means the user-provided number. So The program should accept a number from the user and generate or print all Armstrong numbers up to the given number.

📢 The Program should handle all digits of Armstrong numbers i.e 3-digit and N-digit Armstrong numbers

Here is the expected Input and output of the program.

Example Input and Output:


Enter a Number(To generate armstrong number upto it): 5000


Armstrong numbers upto 5000 are : 1 2 3 4 5 6 7 8 9 153 370 371 407 1634

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

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.

📢 It is recommended to look at the 3-Digit / N-digit Armstrong number program. Where we explained about the Armstrong number in detail.

Here are a few Armstrong numbers for your reference.

DigitArmstrong Numbers
10, 1, 2, 3, 4, 5, 6, 7, 8, 9
3153, 370, 371, 407
41634, 8208, 9474
554748, 92727, 93084
71741725, 4210818, 9800817, 9926315


It is recommended to have knowledge of the C Loops, and Arithmetic Operators to better understand the program. We have already covered these topics in earlier posts, Please go through the following links to learn more about these topics.

Armstrong Numbers upto n in C Program Algorithm:

We are going to use the N-Digit Armstrong number program, As it will cover all digits cases (Including the 3-digit Armstrong numbers).

The generate the N-digit Armstrong numbers, We need to calculate the digit with the power of  nDigits i.e  pow(digit, nDigits).

Program Algorithm:

  1. Start the Program by taking input from the user and storing it in variable n ( We are going to generate all Armstrong numbers up to this number n)
  2. Quickly check if the number n is a Negative number, If so display the error message and terminate the program. Otherwise, proceed to the next step.
  3. As specified earlier, We need to calculate the pow(digit, nDigits) to check the Armstrong number.
  4. So we need to count the number of digits in the given number n. To do that, We use a function called countDigits(), Which takes a number as input and returns the number of digits.- int nDigits = countDigits(temp);
  5. We are going to use two loops, Outer-Loop and Inner-Loop.
  6. The Outer-Loop: This starts from the number 1 and goes all the way up to the number n. We use variable i to control Outer-Loop.
    • Take the back up of i to variable temp. As we need i for later comparisons.
    • The Inner-Loop: Inner-Loop will help us to check if the outer loop variable i is an Armstrong number or not. If the i is Armstrong number, Then it will print it onto the console.
      • At Each Iteration,
      • Get the last digit from the number using rem = temp %10;
      • Calculate the power of rem with nDigits(Number of digits in n) – i.e power(rem, nDigits);. We are using the Power of Number Program in C to calculate the power instead of the pow built-in function. Then Update the arm variable.
      • Remove the last digit using the temp/10 operation.
    • Check if the variable arm is equal to i. If both are equal, Then i is an Armstrong number. Print it onto the console.
  7. The above step-6 will continue until i reaches the given number n. ( Outer Loop Condition – i<=n )
  8. Once the above two loops are terminated, We will get all Armstrong numbers from the number 1 to n. ( user-provided number)

Armstrong Numbers upto n in C Program using Custom Pow function:

We are using a custom power function in this program. You can see the custom power function definition at line 31. We also defined another function countDigits(), which helps us to calculate the number of digits in a number.

Program Output:

Compile the program.

$ gcc armstrong-upto-n.c

As we are using our own power function, We no need to add the -lm compilation option.

📢 If you are using the in-built pow function, Then don’t forget to include the math.h in your C Program. And also pass the -lm option while compiling the program.

Let’s run the program

As you can see from the above output, When a user entered the number n as 5000, The program generated all Armstrong (3-digit and N-digit) numbers up to the number 5000. i.e 1 2 3 4 5 6 7 8 9 153 370 371 407 1634

Let’s look at a few more examples

The program is generating the Armstrong numbers upto the user-provided number correctly. We can see the from the output, The program generated Armstrong numbers up to 100000 properly.


What if the user enters a negative number? Let’s test one test case for it as well.

Program to generate Armstrong Numbers upto n in C using In-Built Pow Function:

We have changed a couple of things in this program. We removed our custom power function from the program. As we have the in-built power function pow. The in-built pow function is available as part of the math.h header file. So we included the math.h header file to our program and calculate the power using the pow(rem, nDigits).

Program Output:

As we are using the pow function from the math.h header file, We need to include the library file. To include the library file, We need to pass the -lm option during the compilation.

$ gcc armstrong-upto-n-inbuilt-pow.c -lm

Run the program.

  1. Collection of C Loops Programs [ List of Loop Practice Programs ]
  2. C Program Check Prime Number [Mutliple Methods]
  3. C Program to Convert Decimal Number to Binary Number
  4. C Program to Convert Binary Number to Decimal Number
  5. C Program to Convert Octal Number to Decimal Number
  6. C Program to Calculate the Factorial of Number
  7. C Program to Find all Factors of a Number
  8. C Program to find all Prime Factors of number 
  9. C Program to Calculate the GCD or HCF of Two Number
  10. C Program to Calculate the LCM of two Numbers
  11. C Program to Check Palindrome Number


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

You may also like...

2 Responses

  1. […] C Program to Generate Armstrong Numbers upto N ( User-Provided Number) […]

  2. […] C Program to Generate Armstrong Numbers upto N ( User-Provided Number) […]

Leave a Reply