**Armstrong Number in C:** A n-digit number is said to be an **Armstrong number** or **Narcissistic number** if the sum of its digits raised to the nth power equals the number itself.

Sequence of Armstrong numbers are: 1,2,3,4,5,…..,9,153….. etc.

**Example:** 153 (n=3) because 1^{3} + 5^{3} + 3^{3} = 1 + 125 + 27 = 153.

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

Write a C program to print all the Armstrong numbers between two intervals.

1. Run a loop from the lower value of range to the upper value of range.

2. Check each number, the sum of all digits raised to power **n** (no. of digits) equals the given number or not.

3. If it is, then it is an Armstrong number. Otherwise, it is not an Armstrong number.

4. Exit.

For a better understanding of the Armstrong number program, consider the following two scenarios.

- Printing all Armstrong Numbers between 1 to 1000
- Printing all Armstrong Numbers between a given specified range

In this approach, we will print all Armstrong Numbers present between the range of 1 to 1000.

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

/* * C Program to Print Armstrong Number from 1 to 1000 */ #include <stdio.h> void main() { int temp, digit1, digit2, digit3; printf("All Armstrong numbers between 1 and 1000 are:\n"); for(int n=1;n<=1000;n++) //suppose loop is at n = 371 { if(n<=9) { printf("%d ",n); } else { digit1 = n % 10; //digit1 = 371 % 10 = 1. digit2 = (n % 100 - digit1) / 10; //digit2 = (371%100 – 1) / 10 = 7 digit3 = (n % 1000 - digit2) /100; //digit3 = (371%1000 –7)/100 = 3 temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 * digit3); //(1*1*1) + (7*7*7) + (3*3*3) = 371 if (temp == n) { printf("%d ", temp); } } } }

In this C program, we are printing the Armstrong number from 1 to 1000. An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself. Hence, 153 because 1^{3} + 5^{3} + 3^{3} = 1 + 125 + 27 = 153.

We run a loop from 1 to 1000. For the first 9 numbers, that is 1 to 9 we print all the numbers as all the numbers from 1 to 9 are Armstrong. For example consider 7 it has only 1 digit so n=1, using the formula, **wxy = pow(w,n) + pow(x,n) + pow(y,n)** we find, pow(7,1) = 7 which is Armstrong. So, all 1 digit numbers are Armstrong.

Using while loop checks the condition that the value of ‘number’ variable is less than or equal to 900. If the condition is true then, execute the iteration of the loop. The ‘digit1’ variable is used to compute the division of the value of ‘number’ variable by 10 and multiply the resulting value by 10 and subtract the multiplied value with the value of ‘number’ variable.

The ‘**digit1**’ variable is used to store the result obtained from the division of the number ‘**n**’ by 10 and get the remainder which is the last bit of ‘**n**’.

**digit1 = n % 10;**

Consider, example 371. So, **digit1** = 371 % 10 = 1.

The ‘**digit2**’ variable is used to store the result obtained from division of the number ‘**n**’ by 100(remainder) then subtracting **digit1** from it, and then finding the quotient by dividing it by 10.

**digit2 = (n % 100 – digit1) / 10;**

**digit2** = (371%100 – 1) / 10 = (71-1)/10 = 70/10 = 7.

The ‘**digit3**’ variable is used to store the result obtained from division of the number ‘**n**’ by 1000(remainder) then subtracting **digit2** from it, and then finding the quotient by dividing it by 100.

**digit3 = (n % 1000 – digit2) /100;**

**digit3** = (371%1000 – 7) / 100 = (371-7)/100 = 364/100 = 3.

The ‘**temp**’ variable is used to multiply the value of **digit1**, **digit2**, and **digit3** variables to the power of 3 respectively. Compute the summation of all the three multiplied values. (1*1*1) + (7*7*7) + (3*3*3) = 371.

If condition statement is used to check the value of ‘**temp**’ and ‘**n**’ variables are equal, if the condition is true print the Armstrong number.

**Time Complexity: O(n)**

The above program for finding Armstrong numbers between 1 to 1000 has a time complexity of O(n) as the loop runs from 1 to 1000.

**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.

All Armstrong numbers between 1 and 1000 are: 1 2 3 4 5 6 7 8 9 153 370 371 407

In this approach we will print all Armstrong Numbers present between a specified range entered by the user.

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

/* C Program to Print Armstrong Number between a given range */ #include <stdio.h> #include <math.h> void main() { int low, high; //for taking user input for range printf("Enter the starting value of the range: "); scanf("%d", &low); printf("Enter the ending value of the range: "); scanf("%d", &high); printf("All Armstrong numbers between %d and %d are:\n", low, high); for(int n=low;n<=high;n++) { int num = n,rem,sum=0; if(n<=9) { printf("%d ",n); } else { int digit = (int) log10(num) + 1; //To count number of digits //Calculating 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; } if (sum == n) { printf("%d ", n); } } } }

In this C Program, we are printing all the Armstrong Numbers between a given range provided by the user.

Here, we use two integer variables **low** and **high** for taking the lower and upper range between which we need to find all the Armstrong Numbers.

We run a for loop starting from **low** and ending at **high**. An another variable **num** is used for storing the values of n. A variable **digit** is used to store the number of digits present in the variable **num**.

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, after calculating sum of power of digits we check if the value of variable **sum** is equal to the original number n or not. If they are equal i.e. **sum==n** then we print the number **n** else we increase the value of **n** by 1 and check for the next number.

**Time Complexity: O(n)**

The above program for finding Armstrong numbers between a given range has a time complexity of O(n), where n = the value high entered by the user.

**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.

Output: Enter the starting value of the range: 150 Enter the ending value of the range: 99999 All Armstrong numbers between 150 and 99999 are: 153 370 371 407 1634 8208 9474 54748 92727 93084

For more information on the Armstrong programs/Examples, 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”.

**Next Steps:**

- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

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