Printing Armstrong numbers from 1 to 500.

Write a program to print out all Armstrong numbers 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.
For example,
153 = (1*1*1) + (5*5*5) + (3*3*3)




#include<stdio.h>
main()
{
int number, temp, digit1, digit2, digit3;

printf("Printing all Armstrong numbers between 1 and 500:\n\n");

number = 001;

while (number <= 500)
{
digit1 = number - ((number / 10) * 10);

digit2 = (number / 10) - ((number / 100) * 10);

digit3 = (number / 100) - ((number / 1000) * 10);

temp = (digit1*digit1*digit1) + (digit2*digit2*digit2) + (digit3*digit3*digit3);

if (temp == number)
{
printf("\nAmstrong Number:%d", temp);

}

number++;
}
}



The file can be found at:
Download File.

Comments

Anonymous said…
Hi Great... We can find the answers but upto 3 digits(999) only!..........Jai..
Farooque said…
Ya, Thanks for that!

But what about numbers above 999????
Unknown said…
hey thanks good and easy programing
Anonymous said…
Is armstrong no only valid for 3 digits?
Unknown said…
Is armstrong no only valid for 3 digits?
Unknown said…
m confused wid d def of armstrong... pls let me kno..
Unknown said…
This comment has been removed by the author.
Unknown said…
(C# Code)

long n, q, r, s, i;
Console.WriteLine("Enter the Number :"); n = Convert.ToInt64(Console.ReadLine());//Printf("enter the Number.");Scanf("%d",&n);
for (i = 0; i <= n; i++)
{ q = i;
s = 0;
while (q > 0)
{ r = q % 10;
s = s + r * r * r;
q = q / 10;
}
if (i == s)
Console.WriteLine("\n"+i+"\n");//This line says to Print 'i' to get desired Reuslt.
} Console.ReadLine();//getch();

check with this program and revert to me on sshakeerbaig@yahoo.com,sshakeerbaig@gmail.com.
Anonymous said…
I m not getting that how r v defining these digits 1,2,3....plz explain !!!
Anonymous said…
#include
#include
#include
void main()
{
int i=100,n,b,s;
clrscr();
for(i;i<=999;i++)
{
s=0;
n=i;

while(n>0)
{
b=n%10;
s=s+b*b*b;
n=n/10;
}
if(s==i)
{
printf("%d\n",s);
}


}

getch();
}
Anonymous said…
use "for looping" insted of while to find ans above 999
Anonymous said…
thanks
Abhishek D. said…
#include
#include
#include
void main()
{
long int a=1,b,c,d=1,e,f=0;
clrscr();
while(d<=20)
{
b=0;
f=0;
c=a;
do
{
c=c/10;
f++;
}
while(c>0);
c=a;
while(c>0)
{
e=c%10;
b=b+pow((double)e,(double)f);
c=c/10;
}
if(b==a)
{
printf("\n%ld",a);
d++;
a++;
}
else
{
a++;
}
}
getch();
}
Abhishek D. said…
1st include is stdio.h
2nd include is conio.h
3rd include is math.h
foram said…
void main()
{
int start,end,i,d,temp,sum=0;
clrscr();
printf("\n Enter number of start and end:");
scanf("%d%d",&start,&end);
printf("\n Armstrong no.are:");
for(i=start;i<=end;i++)
{
temp=i;
sum=0;
do
{
d=temp%10;
sum=sum+d*d*d;
temp=temp/10;
}while(temp!=0);
if(i==sum)
{
printf("%d",i);
}
getch();
}
Unknown said…
I HAVE A GOOD C PROGRAM VIA WHICH U CAN FIND ANY ARMSTRONG NO WITHIN INT RANGE....

AND THATS MUCH SIMPLE THAN THE GIVEN ABOVES!!!!!
Unknown said…
use a for loop 4 counting the digits and then in pow use pow(r,c)..

then it will generate all the armstrongs no withing int range..

4 generation use a while loop at d beggining...

simple.......
prashant said…
#include
main()
{
int i,n,r,sum;

for(i=1;i<=500;i++)
{
n=i;
sum=0;
while(n>=1)
{
r=n%10;
sum=sum + r*r*r;
n=n/10;
}

if(sum==i)
printf("%d \n",i);
}
}
vaishali s said…
this is just complicating a simple program
Anonymous said…
everybody who posts their codes should execute them and post it. there are some blunders in codes.
george harrison said…
for(int a=1;a<=999;a++)
{
num1=a/100;
num2=(a%100)/10;
num3=(a%10);
temp=(num1*num1*num1)+(num2*num2*num2)+(num3*num3*num3);
if(a==temp)
printf("%a is an armstrong number",a);
}
v.rakesh krishnan said…
if anyone cums to know to print exact first 20amstrong no means....pls let me know
Anonymous said…
#include
#include
int armstrong(int a);
int main()
{
int i,z;
for(i=0;i<=499;i++)
{
z=armstrong(i);
if(z==1)
printf("%d is armstrong \n",i);
}
getch();
}


int armstrong(int a)
{
int b,n,n1,result=0;
b=a;
while(b>0)
{
n1=b%10;
result=result+n1*n1*n1;
b=b/10;
}
if(result==a)
return 1;
else
return 0;
}
Rudra d great said…
easy one......

#include
#include

int main()
{
int a,b=0,sum=0,c,d;
printf("ENTER A NUMBER:-");
scanf("%d",&a);
c=a;
while(a>0)
{
b=a%10;
a=a/10;
sum= (b*b*b) + sum;
}
if(sum==c)
printf("armstrong no....");
else
printf("not armstrong");
getch();
return 0;
}
sakura said…
i think cube is for three digits and square for two digits and something like dat...
Anonymous said…
But the first one is to simple.gr8
Anonymous said…
Armstrong numbers are the sum of their own digits to the power of the number of digits. As that is a slightly brief wording, let me give an example:

153 = 1³ + 5³ + 3³

Hence, the program should display nos. from 0-9 also.
sakthivel said…
int a = int.Parse(textBox1.Text);
while (a > 0)
{
j = a % 10;
s = s + (j * j * j);
a = a / 10;

}
if (s==int.Parse(textBox1.Text))
{
MessageBox.Show("Armstrong");
}
else
{
MessageBox.Show("Not a Armstrong");
}
shah bhaumik said…
to get for upto 3 digit you enter s=s+r*r*r*r;
shah bhaumik said…
upto 3 digit num...
enter s=s+r*r*r*r;
Priti said…
But Y did u use if statement?
Anonymous said…
My PROGRAMMING FOR ARMSTRONG NUMBERS (TILL 500).... I did it on my own, that too in my First attempt.... Yippeee....!!!
AARAV KOTHARI (IIT Delhi)

Here we go-

#include
#include
void main()
{
int n,m,l,k,p,a;
printf("ARMSTRONG NUMBERS (1-500)-\n\n1\n");
n=10;
while(n>=10 && n<=99)
{
l=n%10;
m=n/10;
a=l*l*l+m*m*m;
if(a==n)
{
printf("%d\n",n);
}
n++;
}
n=100;
while(n>=100 && n<=500)
{
l=n%10;
p=n/100;
k=n/10;
m=k%10;
a=l*l*l+p*p*p+m*m*m;
if(a==n)
{
printf("%d\n",n);
}
n++;
}
}
Anonymous said…
#include
Haziq said…
Hello, very nice tutorial.
I have made a similar but somewhat different attempt on my blog.
Anonymous said…
with out if statement can we write,think once.u cannot ...
hello.thanks best and easy programming tutorial giving one of the most useful techniques here as publicly
Anonymous said…
Armstrong Defination Wrong
{prog1999} said…
Program to check if a umber is an armstrong number or not...aNY NUMBER OF DIGITS.......GREAT PROGRAM BY {prog1999} (for GW Basic)-------


10 cls
20 input n:n1=n
30 while n<>0
40 D=N mod 10:S=S+d^3
50 n=int(n/10)
60 wend
70 IF S=N1 THEN PRINT "ARMSTRONG_NUMBER_FOUND" ELSE PRINT "ARMSTRONG_NUMBER_NOT_FOUND"
80 END
Anonymous said…
There are only 4 armstrong numbers
152,370,371,407
SAIF said…
Why does the program shows 1 also as an armstrong number?
SAIF said…
Tried this condition in if statement it worked,

if(temp==number && number!=1);
Wonderful brother!! :)
But for the digit 3, instead of writing like this
digit3 = (number / 100) - ((number / 1000) * 10);
you could have simply written
digit3 = number/100;

:)

Popular posts from this blog

C Program - Calculation of Area and Circumference of a Circle using Pointers

Matchstick Game using C Programming

C Program to Calculate Factorial of a number using Recursion