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

Hw Help Real numbers

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);
            }
        }
        
    }
}
Blax
Newbie Poster
8 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

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"

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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

if (a < 0)<strong>;</strong>
{

}

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);
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

When is a going to be less than zero?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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);
                }
        }

        
    }
}
Blax
Newbie Poster
8 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

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

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
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

Blax
Newbie Poster
8 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: