954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help!!! perfect numbers from 1-1000..and its factors

I'm currently making a program that list all the perfect numbers from 1-1000. and list also its factors.

heres my code.

public class perfect
{
public static void main(String[]args)
{
int sum=0;
int x=0;
for(int num=1;num<1000;num++)
{
for(int factor=1;factor<num;factor++){
x=num%factor;
if(x==0)
sum=sum+factor;}
if(sum==num){
System.out.print(num);
System.out.print("The factors are ");
for(int factor=1;factor<num;factor++)
{
x=num%factor;
if(x==0)
System.out.print(factor);
}
}
}
}
}

when i compiled it there is no error but it doesn't display anything...

could someone help me...

comp_sci11
Light Poster
38 posts since Jul 2006
Reputation Points: 14
Solved Threads: 0
 

I'm currently making a program that list all the perfect numbers from 1-1000. and list also its factors.

heres my code.

public class perfect { public static void main(String[]args) { int sum=0; int x=0; for(int num=1;num<1000;num++) { for(int factor=1;factor<num;factor++){ x=num%factor; if(x==0) sum=sum+factor;} if(sum==num){ System.out.print(num); System.out.print("The factors are "); for(int factor=1;factor<num;factor++) { x=num%factor; if(x==0) System.out.print(factor); } } } } }

when i compiled it there is no error but it doesn't display anything...

could someone help me...


Couple edits:

public class PerfectNumbers
{
public static void main(String[]args)
{
int sum=0, x=0;
for(int num=1;num<1000;num++)
{
for(int factor=1;factor<num;factor++){
x=num%factor;
if(x==0)
sum=sum+factor;}
if(sum==num){
System.out.println("\n\n"+num+":");
System.out.println("\nThe factors are: ");


for(int factor=1;factor<num;factor++)
{
x=num%factor;
if(x==0)
System.out.println(factor);
}
}
sum=0;
}
}
}


displays:

6:

The factors are:
1
2
3


28:

The factors are:
1
2
4
7
14


496:

The factors are:
1
2
4
8
16
31
62
124
248
Press any key to continue...
TheGathering
Junior Poster
102 posts since Jul 2007
Reputation Points: 22
Solved Threads: 10
 

i didn't run the code but it seems you need to set sum to zero somewhere in your code. (after you complete factoring a number i guess)

tonakai
Junior Poster
121 posts since Feb 2005
Reputation Points: 25
Solved Threads: 11
 

thanks for the help!! the gathering!!!

i never thought that i just have to set the sum to zero.

it was really a great help!

comp_sci11
Light Poster
38 posts since Jul 2006
Reputation Points: 14
Solved Threads: 0
 

I'm writing a program for C++ and I must be missing something. The program doesn't display the perfect numbers and always gets hung-up one the two red sections.

#include
using namespace std;
bool isPerfect(int Num);


bool isPerfect(int num)
{
int sum = 0;

for (int i = 1; i < num; i++)
{
if (num % i == 0);
{
sum = sum + i;
}
}
if (sum == num)
{
return 1;
}
else
{
return 0;
}
}

int main()
{
cout << "The perfect numbers are:" << endl;
for (int num = 1; num <= 1000; num++)
{
if (isPerfect() == 1)
{
cout << num << " ";
}
}
}

evilserph
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
I'm writing a program for C++ and I must be missing something.

Yes, you are missing the fact that this is someone else's two year old thread and it's in Java, not C++.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

public class Text extends Thread{
public static void main (String argv[])}
Test b=new Test();
b.start();
}
public void run(){
System.out.println("Running");
}
}

gihariwathsala
Newbie Poster
1 post since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You