The Program in C that prints out all Armstrong numbers between 1 and 500 are given below:
#include <stdio.h>
#include <math.h>
int main()
{
int num, originalNum, remainder, n = 0, result = 0;
printf("Armstrong numbers between 1 and 500 are: \n");
for(num = 1; num <= 500; num++)
{
originalNum = num;
while (originalNum != 0)
{
originalNum /= 10;
++n;
}
originalNum = num;
while (originalNum != 0)
{
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
if (result == num)
printf("%d ", num);
n = 0;
result = 0;
}
return 0;
}
Output:
Armstrong numbers between 1 and 500 are:
1 2 3 4 5 6 7 8 9 153 370 371 407
Pro-Tips💡
This program uses two nested while loops to check if each number between 1 and 500 is an Armstrong number.
The outer loop iterates through all the numbers between 1 and 500, and the inner loop checks if the number is an Armstrong number by calculating the sum of its digits raised to the power of the number of digits.
If the number is an Armstrong number, it is printed to the screen.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.