What is the Armstrong number of C?
Armstrong number (also known as narcissistic number, total number, or almighty number) is a number equal to the sum of each digit raised to the power of its digits. For example, 153 is an Armstrong number because:
This number is named after Michael F. Armstrong, who used it as an example of a programming problem in 1969. These numbers are interesting in mathematics and programming because they are relatively rare and are often used in exercises to test programming skills.
Armstrong number example
If the Armstrong number is a positive integer of order n, it can be defined as follows:
abcde…. = pow (a, n) + pow (b, n) + pow (c, n) + pow (d, n) + pow (e, n) + ………
For example : 0, 1, 153, 370, 371, 1634 etc.
Let’s check if 370 is Armstrong’s number.
370 = (3 * 3 * 3) + (7 * 7 * 7) + (0 * 0 * 0)
Here,
(3 * 3 * 3) = 27
(7 * 7 * 7) = 343
(0 * 0 * 0) = 0
27 + 343 + 0 = 370, which is equal to the given number; hence it is an Armstrong number.
Let’s check your four-digit Armstrong number.
1634 = (1 * 1 * 1 * 1) + (6 * 6 * 6 * 6) + (3 * 3 * 3 * 3) + (4 * 4 * 4 * 4)
Here,
(1 * 1 * 1 * 1) = 1
(6 * 6* 6 * 6) = 1296
(3 * 3 * 3 * 3) = 81
(4 * 4 * 4 * 4) = 256
1 + 1296 + 81 + 256 = 1634, which is equal to the given number; hence it is an Armstrong number.
Armstrong number algorithm in C
- Takes input from the user.
- Initialize sum = 0 and use a temporary variable to temporarily store user input (var = num).
- Now find out the total number of digits in the given number.
- The total number of digits is
- Repeat the loop until var > 0.
- Sum up the output of the while loop and store it
- Check if your user number equals the total.
- If the value prints “This is the Armstrong number”
- Otherwise print “Not an Armstrong number”.
Armstrong number program in C
#include <stdio.h>
#include <conio.h>
int main ()
{
int num, var, rem, sum = 0, a = 0 ;
printf ( “ Please enter an integer: “ ); // Taking the user input
scanf ( “%d”, &num );
var = num;
while (var != 0) // Finding the numbers of digits in a given number
{
var = var / 10;
++a;
}
var = num;
while (var > 0 ) // Calculate the number to check it is Armstrong or not
{
rem = var % 10;
sum = sum + pow( rem, a );
var = var / 10;
}
if ( sum == num ) // Check whether the sum is equal to the given number of not
{
printf ( “ %d is an Armstrong number n ”, num );
}
else
{
printf ( “ %d is not an Armstrong number n ”, num );
}
return 0;
}
Output 1:
Please enter an integer: 371
371 is Armstrong’s number.
Output 2:
Please enter an integer: 1045
1045 is Armstrong’s number.
explanation
In the above code, we first declared all the variables needed by the program. num is declared to hold user input. var is used for temporary storage and rem is used to store the remaining parts needed for calculations. Finally, we get the sum assigned to 0 and a.
We accept user input and then assign that number to num. You can then assign this number to var to perform calculations and keep user input safe in the num variable.
Now we calculate the number of digits of user input. For this I assigned num value to var and used a while loop. The while loop runs until (var !=0) and then we can divide var by 10 to calculate the total number of digits in that number. Because var is an integer type, it does not store decimal numbers, and the value of an increases by 1 each time. This process is repeated until var becomes 0. After that we exit the loop.
Now again we assign the value num to var. Then we start a loop and this loop runs until var becomes greater than 0. There are three steps to be performed inside the loop: First find the remainder of the number and then use pow(rem, a) to calculate the power of the remainder. Here rem represents the remainder and a represents the power, i.e. the total number of digits in the number. Calculated above. Now we divide var by 10. This is because the last digit of the number is not needed because we have already used it. Because var is an integer type, it ignores decimal values and stores integer values. This process is repeated until var becomes greater than 0. After that we exit the loop and the final value is stored in the sum.
When control moves out of the loop, the if statement checks whether the sum is equal to num. Here, num is the value entered by the user. If the sum is equal to num, it is Armstrong number. Otherwise it’s not an Armstrong number.
Here’s everything about “Armstrong number in C”. I hope you find this implementation informative. Stay tuned for future blogs. Take our free online C programming course for beginners and learn these concepts in more detail!