Write a program that prints every perfect number from 1 through 1000. A perfect
ID: 3625920 • Letter: W
Question
Write a program that prints every perfect number from 1 through 1000. A perfect number is one that equals the sum of all the numbers that divide evenly into it. For example, 6 is perfect because 1, 2 and 3 divide evenly into it, and their sum is 6. Save the program as Perfect.java(below is code of the program and it is already working)
(plss HELP ME to convert this using while loop..)
public class Perfect{
public static void main(String[] args){
int i,num,sum;
System.out.println("The perfect numbers between 1 and 1000 are");
for(num=1;num<=1000;num++)
{
sum=0;
for(i=1;i<num;i++)
if(num%i==0)
sum+=i;
if(sum==num)
System.out.println(num+" ");
}
}
}
Explanation / Answer
please rate -thanks
public class perfectnumbers
{public static void main(String[] args)
{int i,num,sum;
System.out.println("The perfect numbers between 1 and 1000 are");
num=1;
while(num<=1000)
{sum=0;
i=1;
while(i<num)
{if(num%i==0)
sum+=i;
i++;
}
if(sum==num)
System.out.print(num+" ");
num++;
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.