Program to Generate Nth Armstrong number in C Language


Nth Armstrong number in C Program Description:

Write a C Program to generate the Nth Armstrong number in C language. The program should accept a positive number ( n) from the user and display the Nth (user-provided) Armstrong Number using loops. The program should display an error message on Invalid inputs like negative numbers.

Here is a sample input and output of the program.

Example Input and Output:


Enter a Number(nth armstrong number): 16


16(th) Armstrong number is : 9474

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.

abcd = pow(a, n) + pow(b, n) + pow(c, n) + pow(d, n)

Here n is the number of digits in the original number( abcd), which is 4 as we have 4 digits in abcd.

Here are a few Examples of Armstrong Numbers.

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

Related Armstrong Numbers Programs:

This program is part of the Armstrong numbers programs series.

Pre-Requisites (Knowledge Required):

It is recommended to know the basics of the C Loops like the While Loop, For loop, And Arithmetic operators like Modulus operators, and Division Operators.

Nth Armstrong number in C Program Algorithm:

  1. Take a number from the user and store it in the variable n.
  2. Check if a given number n is a Negative number, If so display the error message and stop the program. Otherwise, proceed to the next step.
  3. 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 to get the Nth Armstrong number.
  6. The Outer-Loop: This starts from the number 1 and continues till we find our Nth Armstrong number, We use cnt variable(default value is 1) to calculate the count of Armstrong numbers. We use the variable i to control Outer-Loop. – for(i=1; cnt<=n; i++)
    • Take the backup of the variable i and store it in 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 increment the counter variable cnt.
      • At Each Iteration,
      • Get the last digit of the number ( temp) i.e rem = temp %10;
      • Calculate the power of rem with nDigits(Number of digits in i or temp) – i.e power(rem, nDigits);. We can use the inbuilt pow function or We can use a custom Power of Number Program in C to calculate the power of a number.
      • Then Update the arm variable. – arm = arm + pow(rem, nDigits);
      • 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.
      • Check our cnt is equal n (Nth Armstrong number), If the cnt is equal to n, We got our Nth Armstrong number. So print the result onto the console.
      • Else increment the cnt variable.
  7. The above step-6 will continue until we find our Nth Armstrong number.

Program to generate Nth Armstrong number in C Language:

We are using the C language’s in-built pow function in this program.

Don’t forget to include the math.h headerfile to the program.

Program Output:

📢 As we are using the math.h headerfile, use the -lm compilation flag to include the library. If you forgot to include the -lm compilation option, You will get a Linking error.

Compile and run the program using the GCC compiler.

$ gcc nth-armstrong.c  -lm

Note, We passed -lm option to GCC. Run the program.

The first 9 Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, and 9. So the 10th Armstrong number is 153. Please check the above table, where we listed the Armstrong numbers up to 10000000.

Let’s try a few more examples.


Negative numbers test case

As expected, Program is displaying an error message, If the user enters a negative number.

Calculate Nth Armstrong number in C using the Custom Power function:

Let’s use our custom power function instead of the in-built power function. Here is the prototype of the custom power function.

double power(double base, double exponent);

As we are not going to use the math.h header file pow function, We can remove the math.h header file from our program.

Program Output:

Compile the program. We no need to include the -lm compilation flag ( as we are not using the math.h)

$ gcc nth-armstrong-custom.c

Run the program. ( ./a.out ). Let’s, try with the positive numbers.

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

Let’s try with the negative numbers.


As expected, The program is displaying the error message ( Error: Please enter positive numbers ) when the user enters a Negative number.

Related Programs:

  1. C Loops Practice Programs Index
  2. C Program to Calculate Nth Prime Number
  3. C Program Check Prime Number [Mutliple Methods]
  4. C Program to Convert Decimal Number to Binary Number
  5. C Program to Convert Binary Number to Decimal Number
  6. C Program to Convert Octal Number to Decimal Number
  7. C Program to Calculate the Factorial of Number
  8. C Program to Find all Factors of a Number
  9. C Program to find all Prime Factors of number 
  10. C Program to Calculate the GCD or HCF of Two Number
  11. C Program to Calculate the LCM of two Numbers
  12. 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...

1 Response

  1. […] C Program to Generate Nth Armstrong Number […]

Leave a Reply