A positive integer of n digits is said to be an Armstrong number of order n if the sum of its digits raised to the nth power equals the number itself.

Armstrong Number Formula: **wxyz… = pow(w,n) + pow(x,n) + pow(y,n) + pow(z,n) + …..**

**Example:** 370 (n=3) because 3^{3} + 7^{3} + o^{3} = 27 + 343 + 0 = 370.

Write a C program to find the nth Armstrong number.

1. Ask the user to enter a number.

2. Assign a variable count = 1.

3. Run a loop from i=1 and check for each value of i whether it is Armstrong or not.

4. If it is, then checks if the value of count and n are equal.

5. If they are equal return the number i.e. value of i;

6. Else increment the value of count by 1;

7. Exit.

**For example**,

The first 15 numbers in the Armstrong sequence are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, and 8208.

2nd Armstrong number is 2.

10th Armstrong number is 153.

13th Armstrong number is 407.

Here is the source code of the C Program to find the nth Armstrong number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C Program to find the nth Armstrong number */ #include<stdio.h> #include<stdlib.h> #include<math.h> void main() { int n, count=1; printf("*******Value of n should be maximum 31*******"); printf("\nPlease Enter n to find nth Armstrong Number = "); scanf("%d",&n); if(n>31) { printf("\nInteger range of Armstrong Number exceeded. \nPlease enter the value of n less than or equal to 31."); exit(1); } for(int i = 1; i <= 1000000000; i++) { int num=i, rem, sum=0; int digit = (int) log10(num) + 1; //To count number of digits //Calculating the sum of power of digits of a number while(num > 0) { rem = num % 10; //To find last digit of the number. sum = sum + pow(rem,digit); num = num / 10; } // Check for Armstrong number if(i == sum) { if(count==n) { printf("%d\n",i); break; } else { count++; } } } }

In this C program, we are finding the Nth Armstrong number. An Armstrong number is a positive integer of n-digit such that the sum of its digits raised to the power n is the number itself.

We run a loop from **i** = 1 to 1000000000. For each number ‘**i**’ first store its value into another variable ‘**num**’. Find the count of digits present in the number ‘**num**’ and store it in another variable ‘**digit**’.

Assign another variable **count = 1** to keep the count of the Armstrong Numbers being generated. It will keep the count of total Armstrong Numbers generated until we get Nth Armstrong number.

We run a while loop until the value of the ‘**num**’ variable is greater than 0. The ‘**rem**’ variable is used to compute the last digit of the of the ‘**num**’ variable and the ‘**sum**’ variable is used to compute the sum of the power of the digits of variable ‘num’.

Now, we check if the value at variable ‘**i**’ is Armstrong or not. If ‘**i == sum**’ then the number is Armstrong, but we need to find Nth Armstrong Number so, we will check if the value of the variable ‘**count**’ equals to ‘**n**’ or not. If they are equal then print the value at variable ‘**i**’ else increment the value of count by 1.

**Time Complexity: O(k)**

The above program for finding Nth Armstrong number has a time complexity of O(1000000000) = O(k) as the loop runs from 1 to 1000000000, where k = 1000000000.

**Space Complexity: O(1)**

In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

**Program Output 1:**

*******Value of n should be maximum 31******* Please Enter n to find nth Armstrong Number = 13 407

**Program Output 2:**

*******Value of n should be maximum 31******* Please Enter n to find nth Armstrong Number = 33 Integer range of Armstrong Number exceeded. Please enter the value of n less than or equal to 31.

For more information on the Armstrong programs, visit “Armstrong Number in C“.

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for C Internship