C Program to print Armstrong Numbers between 1 and 500


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



YouTube Link: https://www.youtube.com/watch?v=PthvSOapNxc [Watch the Video In Full Screen.]


Source Code: C Program to print Armstrong Numbers between 1 and 500

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     int num, count = 1, rem, sum;  
  6.   
  7.     while(count <= 500)  
  8.     {  
  9.         num = count;  
  10.         sum = 0;  
  11.   
  12.         while(num)  
  13.         {  
  14.             rem = num % 10;  
  15.             sum = sum + (rem * rem * rem);  
  16.             num = num / 10;  
  17.         }  
  18.   
  19.         if(count == sum)  
  20.         {  
  21.             printf("%d is a Armstrong number\n", count);  
  22.         }  
  23.   
  24.         count++;  
  25.     }  
  26.   
  27.      return 0;  
  28. }  

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

Leave a Reply

Your email address will not be published. Required fields are marked *