C program to find and print armstrong numbers between 1 and 1000

Armstrong numbers:

Input : 153.
Calculating armstrong number: 153 =(1*1*1)+(5*5*5)+(3*3*3)= 1+125+27 =153 

If input equals output, 153==153, 153 is armstrong number.

#include<stdio.h>
#include<conio.h>
void main()
{
  int num, n, rem;
  int cube=0, sum; 
  printf("n Armstrong numbers between 1 and 1000 are: n");
  for(num=1;num<=1000;num++)
  {
    sum=0;
    n=num;
    while(n>0)
    {
      rem=n%10;
      cube=rem*rem*rem;
      sum=sum+cube;
      n=n/10;
    }
    if(num==sum)
    {
      printf("n%d is armstrong number",num);    
    }
  getch();
}

Ouput:

Armstrong numbers between 1 and 1000 are:
1 is armstrong number
153 is armstrong number
370 is armstrong number
371 is armstrong number
407 is armstrong number

 

Leave a Reply

error: Content is protected !!
%d bloggers like this: