## INDEX

### C program to print Armstrong numbers from 1 to 500

C program to print Armstrong numbers from 1 to 500

#include<stdio.h>
int main(){
int num,r,sum,temp;

for(num=1;num<=500;num++){
temp=num;
sum = 0;

while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}

return 0;
}

Output:
1 153 370 371 407

Algorithm:

Definition according to c programming point of view:
Those numbers which sum of the cube of its digits is equal to that number are known as Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
Other Armstrong numbers: 370,371,407 etc.
In general definition:
Those numbers which sum of its digits to power of number of its digits is equal to that number are known as Armstrong numbers.
Example 1: 153
Total digits in 153 is 3
And 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Example 2: 1634
Total digits in 1634 is 4
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634

Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725

Anonymous said...

what is wrong in following code??
#include
#include
main()
{
int i;
for(i=1;i<=500;i++)
{
if(i==(((i%10)^3)+(((i/10)%10)^3)+((((i/10) /10)%10)^3)))
printf("%d \n",i);
else continue;
}

}

Devavrat Mathur said...

The only flaw that I see is that c does not have ^ operator.

Unknown said...

@Anonymous first of all I looked up on your code and I tried to fix some issues, the result is almost good, but it's show just one number, 407.

Here is your code a litle bit updated:
main()
{
int i;
for(i=1;i<=500;i++)
{
if(i==(((i%10)*(i%10)*(i%10))+((((i/10)%10)*(i/10)%10)*(i/10)%10)+((((i/10) /10)%10)*(((i/10) /10)%10)*(((i/10) /10)%10))))
printf("%d \n", i);
else continue;
}

}

bandaru nikas said...

here output is 1,407;
i had modified it as

#include

int main()
{
int n,a,b,c,d,e,f,g,h;
for (h=001;h<=500;h++){
n=h;

a=n%10;
b=n%100;
c=(b-a)/10;
d=(n-b)/100;
e=a+c+d;

if (n==(a*a*a)+(c*c*c)+(d*d*d))
{
printf("%d\n",n);
continue;
}

}
}

output is:
1
153
370
371
407

Unknown said...

Thanku sir

Unknown said...

What wrong following code
Main()
{
int i;
For(i=1;i<=500;i++)
{if==(((i%10)^3)+(((i=10)%10)^3)+((((>/10)/10)%10)^3))))
Printf("%d\n",i);
else
}
}

Life journey said...

Is it working

Anonymous said...

#include
#include
int main()

{
int a,b,c,n,i;
for(i=1;i<=500;i=i+1)
{

a=(i%10);
b=((i-a)/10)%10;
c=((i-10*b-a)/100)%10;
n=pow(a,3)+pow(b,3)+pow(c,3);
if(n==i)
printf("\n %d ",i);
else
continue;

}

}