Hello ive been working on this real number program for a couple of days and i still cant get it working it always prints out the same two numbers 1 2 1 2. Please help.

public class perfectnums
{
    public static void main(String[] args)
    {
        int n = 1;
        int a;
        int t = 2;
        int c = 2^(t-1)*(2^t-1);
       
        while ( n < 5 )
        {
          t++;
          c = 2^(t-1)*(2^t-1);
          n++;
          a = c;
        if (a < 0);
            {
              System.out.println(a);
            }
        }
        
    }
}

Recommended Answers

All 6 Replies

I'm not real sure what this is SUPPOSED to do, but try this:

public class perfectnums
{
	public static void main(String[] args)
	{
		int t = 2;
		int c = 0;

		for (int n=1; n < 5; n++ )
		{
			t++;
			c = 2^(t-1)*(2^t-1);

			if (c > 0)
			{
				System.out.println(c);
			}
		}
	}
}

Notice the removal of the extra variables, the change in the test for c being GREATER than ZERO and the change of the "while" to a "for"

Actually what was the problem with the original code was this:

if (a < 0)[B];[/B]
{

}

Semicolon are used to terminate commands. So when you put that semicolon, you terminated the if statement, and rest was printed anyway.
This code is acceptable:

{
 // commands;
}
{
// other commands;
}

Meaning that you can open and close brackets without having to add 'if' or 'while'.
So the ';' terminated the if (); and rest code was outside the if and it was executed anyway.
What you wrote:

if (a < 0);
{
}

Was like writing this:

if (a<0) {}
System.out.println(a);

Or this:

if (a<0) {}
{System.out.println(a);}

When in fact you needed this:

if (a<0) {
   System.out.println(a);
}

or

if (a<0)
   System.out.println(a);

When is a going to be less than zero?

Well i redid it and this is what i wrote

public class perfectnums
{
    public static void main(String[] args)
    {
        int n = 1;
        int a;
        int t = 0;
        int c;
        int counter;

        while ( n < 8 )
        {
          n++; // The number of times the program will run
          t++; // The Power's in equation C
          c = (int)Math.pow( 2 , t - 1 ) * ((int)Math.pow(2,t)-1); // Declaration of equation in while statement
          counter = 0;
         for (int i=1; i<c ; i++)
            {
            a = c%i;
                if (a==0)
                   {
                      counter = counter + i;
                   }

            }
                if (counter == c)
                {
                    System.out.println(c);
                }
        }

        
    }
}

Well does it compile?
Do you get any errors?
What is the problem?

Well does it compile?
Do you get any errors?
What is the problem?

Yes it does compile
There is no errors any more
and the problem has been fixed
and thank you very much for your help

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.