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.
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
But what about numbers above 999????
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.
#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();
}
#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();
}
2nd include is conio.h
3rd include is math.h
{
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();
}
AND THATS MUCH SIMPLE THAN THE GIVEN ABOVES!!!!!
then it will generate all the armstrongs no withing int range..
4 generation use a while loop at d beggining...
simple.......
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);
}
}
{
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);
}
#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;
}
#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;
}
153 = 1³ + 5³ + 3³
Hence, the program should display nos. from 0-9 also.
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");
}
enter s=s+r*r*r*r;
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++;
}
}
I have made a similar but somewhat different attempt on my blog.
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
152,370,371,407
if(temp==number && number!=1);
But for the digit 3, instead of writing like this
digit3 = (number / 100) - ((number / 1000) * 10);
you could have simply written
digit3 = number/100;
:)