Write a program to print out all Armstrong numbers or Narcissistic number between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.
Related Read:
Nested While Loop: C Program
C Program to Check Armstrong Number
For Example:
407 = (4*4*4)+(0*0*0)+(7*7*7)
407 = (64) + (0) + (343)
407 = 407
Hence, 407 is a Armstrong number.
Nested While Loop
In this program we are using nested while loop to check for Armstrong numbers from 1 to 500.
Outer while loop loops from 1 to 500, by incrementing value of count by 1. Inner while loop checks every value of count to determine if the value is a Armstrong number or not.
Logic for Finding Armstrong Number
We’ve explained the logic to find Armstrong number in detail, in our previous video tutorial. Kindly visit C Program to Check Armstrong Number
C Program to print Armstrong Numbers between 1 and 500
Source Code: C Program to print Armstrong Numbers between 1 and 500
#include < stdio.h >
int main()
{
int num, count = 1, rem, sum;
while(count <= 500)
{
num = count;
sum = 0;
while(num)
{
rem = num % 10;
sum = sum + (rem * rem * rem);
num = num / 10;
}
if(count == sum)
{
printf("%d is a Armstrong number\n", count);
}
count++;
}
return 0;
}
Output:
1 is a Armstrong number
153 is a Armstrong number
370 is a Armstrong number
371 is a Armstrong number
407 is a Armstrong number
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert