Assignment program
Title
Question
I am not able to make a program that can show all the Armstrong number between 100 to 1000.
This is where I got so far;
public class ForLoop {
public static void main(String[] args) {
int i;
int j;
int a=1;
int b=1;
int c=1;
int r;
r = a*100+b*10+c;
c= c*c*c;
a= a*a*a;
b= b*b*b;
j=a+b+c;
for (i=100;i<1000;i=i+1) {
if ((i==j)&(i==r)){
System.out.println(i+"is a Armstrong number");
}
}
}
}
above code give the answer but need to put every digit myself to do that.
Java For-Loop 05-06 min 20-30 sec
Answers:
A meaningful program can be written as follows.
public class Armstrong
{public static void main(String[] args)
{
int n, count = 0, a, b, c, sum = 0;
System.out.print("Armstrong numbers from 100 to 1000:");
for(int i = 100; i <= 1000; i++)
{
n = i;
while(n > 0)
{
b = n % 10;
sum = sum + (b * b * b);
n = n / 10;
}
if(sum == i)
{
System.out.print(i+" ");
}
sum = 0;
}
}
}
public class Armstrong
{
public static void main(String[] args)
{
int n, count = 0, a, b, c, sum = 0;
System.out.print("Armstrong numbers from 100 to 1000:");
for(int i = 100; i <= 1000; i++)
{
n = i;
while(n > 0)
{
b = n % 10;
sum = sum + (b * b * b);
n = n / 10;
}
if(sum == i)
{
System.out.print(i+" ");
}
sum = 0;
}
}
}
{
public static void main(String[] args)
{
int n, count = 0, a, b, c, sum = 0;
System.out.print("Armstrong numbers from 100 to 1000:");
for(int i = 100; i <= 1000; i++)
{
n = i;
while(n > 0)
{
b = n % 10;
sum = sum + (b * b * b);
n = n / 10;
}
if(sum == i)
{
System.out.print(i+" ");
}
sum = 0;
}
}
}
Login to add comment