How to Write a Program for an Armstrong Number in C using While Loop, For Loop, Functions, and Recursion? We will also show you the Program for printing the Armstrong Numbers between 1 and n.
If the given number is equal to the sum of the power of n for each digit (own) present in that integer, then that can be an Armstrong Number in C programming. For example, 153 is an Armstrong Number because the total of the individual digits in 153 = 3
153 = 1³ + 5³ + 3³
= 1 + 125 + 27 = 153
The steps below will demonstrate the standard approach to checking for an Armstrong Number in C programming.
- Enter any value.
- Count the total number of digits in a given value. For example, 370 has 3 digits, and 1634 has 4 digits.
- Divide the given one into individual digits (For Example, divide 153 into 1, 5, and 3).
- Calculate the power of n (comes from step 2) for each individual and add those digits to the Sum variable.
- Compare the original value with the Sum value. If they exactly match, then it is an Armstrong number. Otherwise, it is not.
Program for a three-digit Armstrong Number in C
This program checks whether the user-given three-digit number (integer value) is an Armstrong number. Remember, this approach will only check and work for three-digit numbers. If the user enters a large number (greater than 999), it returns wrong results.
The following are the steps that we followed to write the Armstrong number program in C language.
- In the first step, we declared n, t, r, and Sum variables, where Sum is assigned to 0. The printf statement asks the user to enter any three-digit number. The scanf statement stored the user-entered value into the n variable.
- In the second step, assign the user-entered value to a temporary variable (t) to keep the original value unchanged.
- The third step of finding an Armstrong number is about executing a while loop. Here, the while loop condition will continue to iterate until the t value becomes less than or equal to 0. It means repeat the below-mentioned three steps until t becomes 0.
- r = t % 10; – It extracts the last digit from the given number.Sum = Sum + (r * r * r); – As this program is meant for only three-digit numbers, we simply find the cube of the remainder (last digit). Next, add that value to the Sum variable.
- t = t / 10; – Remove the last digit from the t variable.
- The if else statement checks whether the sum of the cubes of every digit in a given number is exactly equal to the original value. If True, this C program prints that it is an Armstrong number. Otherwise, it is not.
TIP: We can also use the math pow function available in the math library. For this, include <math.h> header file at the top and replace (r * r * r) with pow(r, 3).
#include <stdio.h>
int main(void)
{
int n, t, r, Sum = 0;
printf("Please Enter 3-Digit Number = ");
scanf("%d", &n);
t = n;
while( t > 0)
{
r = t % 10;
Sum = Sum + (r * r * r);
t = t / 10;
}
if ( n == Sum )
printf("%d is an Armstrong Number.", n);
else
printf("%d is not an Armstrong Number.", n);
return 0;
}
Please Enter 3-Digit Number = 370
370 is an Armstrong Number.
If you observe the output, we have entered 371 as the integer value. It means it is a three-digit number. 371 = 3 * 3 * 3 + 7 * 7* 7 + 1 * 1 * 1 = 27 + 343 + 1 = 371. So, it is an Armstrong number. However, if you try 999 or 1224, it returns the result of the else statement.
NOTE: If you try 1634, even though it is an Armstrong number, the program will return the message: not.
C Program to Check Armstrong Number for N-Digit Numbers
In the above-mentioned approach, we calculate the cube of each digit because we expect the user to enter a three-digit number. However, there are Armstrong numbers greater than 999, and the above approach is not ideal to check them.
This entire C program section covers all possible options, checking for Armstrong numbers for an N-digit number (0 to n). To do this, we need an extra loop to count the total number of digits in a number. By this, instead of calculating the cube of each digit, we can calculate the digit raised to the power of n (instead of 3).
C Program to Check Armstrong Number using While Loop
In this example, we declared all the required variables at the beginning, including Number, Temp, Reminder, Times, and Sum. Next, this program allows the user to enter any positive integer and check whether it is an Armstrong number or not using the While Loop.
In this C program for Armstrong number example, we declared all the required variables at the beginning, including Number, Temp, Reminder, Times, and Sum. Among them, the Number variable is to store (hold) the user-provided value. The Temp variable acts as a temporary storage of a number value, preserving the original value as it is. The Reminder value stores the last digit value on each iteration. Next, set the Times and Sum values to zero because Times stores the total number of digits in a number. On the other hand, the Sum variable stores the sum of digit power to the total number of digits.
#include <stdio.h>
#include <math.h>
int main(void)
{
int Number, Temp, Reminder, Times =0, Sum = 0;
printf("Please Enter number to Check = ");
scanf("%d", &Number);
//Helps to prevent altering the original value
Temp = Number;
while (Temp != 0)
{
Times = Times + 1;
Temp = Temp / 10;
}
Temp = Number;
while( Temp > 0)
{
Reminder = Temp %10;
Sum = Sum + pow(Reminder, Times);
Temp = Temp /10;
}
printf("Sum of entered number is = %d\n", Sum);
if ( Number == Sum )
printf("%d is Armstrong Number.\n", Number);
else
printf("%d is not.\n", Number);
return 0;
}
Please Enter number to Check = 1634
Sum of entered number is = 1634
1634 is Armstrong Number.
In this C program for Armstrong number, we assign the original value to the Temp variable. It will help us to preserve our actual value and then do all the manipulation on the Temp variable.
The While loop will ensure that the given value is greater than 0. The statements inside the while loop will split the value and count the total individual digits inside the given value.
If you don’t understand this program logic, please refer to the C Program to Count Number of Digits article. The second while Loop will ensure that the given integer is greater than 0. Let us examine the working principle of this C Programming while loop in an iterative manner.
For this Armstrong number in C demonstration, User Entered value: Number = 1634 and Sum = 0.
Temp = Number = 1634
First Iteration
Reminder = Temp %10
Reminder = 1634 % 10 = 4
Sum = Sum + pow (Reminder, Times)
For this example, Times = 4 because the digits in 1634 = 4. The pow() function in this Armstrong number will multiply the Reminder 4 times.
Sum= 0 + 44 => 0 + 256
Sum = 256
Temp = Temp / 10 = 1634 /10
Temp = 163
NOTE: If the digit count is 5, the Reminder will be multiplied by 5.
Second Iteration: From the first iteration, the values of both the Temp and Sum have changed as Temp = 163 and Sum = 256
Reminder = 163 % 10 = 3
Sum = 256 + 34 => 256 + 81 => 337
Temp = 163 /10 = 16
C Armstrong Number Program Third Iteration: Temp = 16 and Sum = 337
Reminder = 16 % 10 = 6
Sum = 337 + 64 => 337 + 1296 => 1633
Temp = 16 /10 = 1
Fourth Iteration: Temp = 1 and Sum = 1633
Reminder = 1 % 10 = 0
Sum = 1633 + 1 => 1634
Temp = 1/10 = 0
Here, temp = 0. So, the while loop condition in this C program for Armstrong Number will fail.
if (Number== Sum ) – Condition will check whether the user entered value is exactly equal to Sum or not. This condition is True, and it is, else it is not.
if(1634 == 1634) – TRUE.
NOTE: If finding the number below 1000, remove the while loop to count the digits. Next, replace the code below.
Sum = Sum + pow(Reminder, Times);
With
Sum = Sum + (Reminder * Reminder * Reminder)
C Program for Armstrong Number using For Loop
This program allows the user to enter any positive integer. Then, this program uses a for loop to check whether a number is Armstrong or not.
In the first for loop, the counter will iterate over the digits in a number until the t value becomes zero. While iterating, it divides the t value by 10 to remove the last digit from the actual value on each iteration. If there are three digits, by the third iteration, it empties the t-value. So, for the fourth, the condition fails. By the end of this for loop, you will get the total number of digits in a given number.
The second for loop initialization, condition, and increment/decrement operators are the same as the first for loop. However, within the for loop, extract the last digit on each iteration and calculate that digit to the power of the value that came from the first for loop. The remaining process is the same as the C program for Armstrong number using a while loop that we explained earlier.
// using For Loop
#include <stdio.h>
#include <math.h>
int main()
{
int n, t, rem, Times =0, Sum = 0;
printf("Please Enter any Number = ");
scanf("%d", &n);
for(t = n; t != 0; t = t / 10)
{
Times = Times + 1;
}
for(t = n; t > 0; t = t / 10)
{
rem = t % 10;
Sum = Sum + pow(rem, Times);
}
printf("Total = %d\n", Sum);
if ( n == Sum )
printf("%d an Armstrong Number.", n);
else
printf("%d is not an Armstrong Number.", n);
return 0;
}
NOTE: We replaced the While loop in the above example with the For loop. Please refer to the For Loop in C Programming to understand this program.
OUTPUT

C Program for Armstrong Number using Functions
This program allows the user to enter any positive integer. Next, this program checks whether the number is Armstrong or not using functions.
// using Functions
#include <stdio.h>
#include <math.h>
int CheckArmstrong(int n)
{
int temp = n;
int Reminder, Sum = 0;
int times = log10(temp) + 1;
for(; temp > 0; temp = temp /10 )
{
Reminder = temp % 10;
Sum += pow(Reminder, times);
}
return (Sum == n);
}
int main()
{
int num;
printf("Please enter any value: ");
scanf("%d", &num);
if (CheckArmstrong(num))
printf("%d is an Armstrong Number.", num);
else
printf("%d is not an Armstrong Number.", num);
return 0;
}
OUTPUT
Please enter any Number = 8208
Sum = 8208
8208 is an Armstrong Number
Within this program, when the compiler reaches the Sum = CheckArmstrong (num); line in the main() program, then the compiler will immediately jump to the following Function:
int CheckArmstrong (int n)
We already explained the LOGIC of this one in the first C program for Armstrong Number example. However, within the function, instead of using an extra loop to count the total number of digits in a number, we used the log10 function. The log10(temp) + 1 returns the total number of digits in a number. Therefore, we can calculate the power of each digit in this number.
NOTE: If we create a function with Void, there is no need to return any value. However, if we declare a function with any data type (int, float, etc.), we have to return something from the function.
C Program to find an Armstrong Number using Recursion
This program allows entering a positive integer and uses the Recursion concept to check whether a number is Armstrong or not. We can use the above-mentioned function concept and convert it into a recursive method by calling the same function recursively.
Here, the IsArmstrong() function accepts two arguments: the original number and the total number of digits in a number. This C program for Armstrong number uses the IsArmstrong (n /10, t) statement inside a function. This statement will help call the function Recursively with the updated value. It means for every function call, it removes the last digit from a given number. If you miss this statement after completing the first line, it will terminate. For example, if n = 153, then the output will be 27.
Let’s see the if condition of this program. The if (n > 0) condition will check whether n is greater than 0 or not. For Recursive functions, placing a condition before using the function recursively is very important. Otherwise, we will end up in infinite execution (like an infinite loop).
#include <stdio.h>
#include <math.h>
int IsArmstrong (int n, int t)
{
int Reminder;
if (n > 0)
{
Reminder = n %10;
return pow(Reminder, t) + IsArmstrong (n /10, t);
}
else
return 0;
}
int main(void)
{
int n;
printf("Please Enter Number = ");
scanf("%d", &n);
int times = log10(n) + 1;
if ( n == IsArmstrong (n, times))
printf("%d is an Armstrong Number.", n);
else
printf("%d is not an Armstrong Number.", n);
return 0;
}
OUTPUT
Please enter Number = 153
Sum of entered is = 153
153 is an Armstrong Number.
Using a Numeric String
This C program example converts the given number into a numeric string and checks whether it is an Armstrong number or not. By converting the number to a string, we can use the length() function or the sizeOf operator technique to find the total number of digits in a number.
The for loop iterates the string from the first character to the last. Within the loop, we use the index position to access each character (digit) from the first position to the last position. It means the first iteration rem value is 5, on the second 4, followed by 7, 4, and 8.
#include <stdio.h>
#include <math.h>
int main() {
int n = 54748;
char str[20];
int rem, sum = 0;
int count = snprintf(str, sizeof(str), "%d", n);
for (int i = 0; i < count; i++)
{
rem = str[i] - '0';
sum += pow(rem, count);
}
if ( n == sum )
printf("%d is an Armstrong Number.", n);
else
printf("%d is not Armstrong Number.", n);
return 0;
}
54748 is an Armstrong Number.
C Program to Print Armstrong Numbers between 1 and 1000
This program checks every number between 1 and 1000 (less than 1000) to find whether it is an Armstrong number. If true, print that number as an output. As we know, the lower (10) and upper bound (1000), we use a for loop to loop over the numbers from 1 to 1000.
#include<stdio.h>
#include <math.h>
int IsArmstrong (int n)
{
int Reminder, Sum = 0;
for(; n > 0; n /= 10 )
{
Reminder = n % 10;
Sum += pow(Reminder, 3);
}
return Sum;
}
int main(void)
{
int n;
printf("Armstrong Numbers Between 10 and 1000 are:\n");
for(n = 10; n < 1000; n++)
{
if(n == IsArmstrong(n))
{
printf("%d ",n);
}
}
}
OUTPUT
Armstrong Numbers Between 10 and 1000 are:
153 370 371 407
C Program to Print Armstrong Numbers from 1 to N
This program allows you to enter minimum and maximum values. This program finds and prints Armstrong Numbers between the Minimum and Maximum values.
The For Loop helps the compiler iterate between Minimum and Maximum Variables. Iteration starts at the Minimum, and then it will not exceed the Maximum variable.
if(n==IsArmstrong(n)) -– condition will check whether the value in each iteration is precisely equal to the sum of the individual digits raised to the power of n or not. And, if this condition is True, then it is Armstrong. Otherwise, it is not. So, if this condition is True, the print statement will print.
#include<stdio.h>
#include <math.h>
int IsArmstrong (int n)
{
int Temp, Reminder, Times, Sum = 0;
Times = log10(n) + 1;
for(Temp = n; Temp > 0; Temp = Temp /10 )
{
Reminder = Temp %10;
Sum += pow(Reminder, Times);
}
return Sum;
}
int main(void)
{
int n, Minimum, Maximum;
printf("Enter the Minimum & Maximum Values = ");
scanf("%d %d",&Minimum, &Maximum);
printf("Armstrong Numbers Between %d and %d are:\n",Minimum, Maximum);
for(n = Minimum; n <= Maximum; n++)
{
if(n == IsArmstrong(n))
{
printf("%d ",n);
}
}
}
OUTPUT
Enter the Minimum & Maximum Values = 10 100000
Armstrong Numbers Between 10 and 100000 are:
153 370 371 407 1634 8208 9474 54748 92727 93084
