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...

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...

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)

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!

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 <iostream>
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 << " ";
        }
    }
}

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++.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.