# Java ^2

• (2 Pages)
• 1
• 2

## 17 Replies - 1499 Views - Last Post: 18 March 2009 - 08:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=93816&amp;s=dea46663571aa8ff0c36de24629b059a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 veronica 2707

Reputation: 0
• Posts: 19
• Joined: 17-March 09

# Java ^2

Posted 18 March 2009 - 06:41 PM

well...i`m doing some exercises on displaying square numbers from 1 to 5 in a loop.
i`ve written the code but its only displaying 1,2,3,4,5

here is the source code:

```public class Loopy {
public static void main(String[] args) {
int x = 1, sqt;
sqt = (x)^2;
System.out.println("Display Numbers from 1 to 5");
while (x <= 5) {
System.out.println("In the Loop");
System.out.println("Value of x is " + x);
x = x + 1;
}
System.out.println("This is after the loop");
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Java ^2

### #2 Daegan

Reputation: 5
• Posts: 67
• Joined: 17-March 09

## Re: Java ^2

Posted 18 March 2009 - 06:53 PM

You aren't multiplying the numbers together.

You should use a number to track the 1-5 while loop, and another int variable to do the multiplication.

your sqt = (x)^2 won't work.

Try adding these to your code. You should be able to figure out where
```int y = 1;

```

```y = x * x;

```

This post has been edited by Daegan: 18 March 2009 - 06:57 PM

### #3 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Java ^2

Posted 18 March 2009 - 07:01 PM

btw you dont calculate the power using ^, the ^ is the XOR operator(bitwise operator)..
this should do it for you
```int x = 1;
System.out.println("Display Numbers from 1 to 5");
while (x <= 5) {
System.out.println("In the Loop");
System.out.println("Value of x^2 is " + (x*x));
x = x + 1;
}
System.out.println("This is after the loop");

```

This post has been edited by mostyfriedman: 18 March 2009 - 07:03 PM

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Java ^2

Posted 18 March 2009 - 07:01 PM

In Java the ^ operator is a bit wise exclusive or
it as nothing, but really nothing, to do with squaring

and you don't apply it to x inside the loop so you just print x = 1,2,3,4,5
printing x ^ 2 you give you other results but nothing related to squaring

### #5 Daegan

Reputation: 5
• Posts: 67
• Joined: 17-March 09

## Re: Java ^2

Posted 18 March 2009 - 07:04 PM

Yeah or those methods

I always overlook the most obvious of solutions.

### #6 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: Java ^2

Posted 18 March 2009 - 07:12 PM

Alternatively...

Math.pow(base, exponent)

Used like...Math.pow(2, 4) = 2 to the 4th power = 16

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Java ^2

Posted 18 March 2009 - 07:20 PM

I should ask SkyHawk: "Are the posts, where the originator seems to have a female name, receive more replies than the ones where the DIC name seems to be a male ?"

### #8 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Java ^2

Posted 18 March 2009 - 07:22 PM

pbl, on 18 Mar, 2009 - 06:20 PM, said:

I should ask SkyHawk: "Are the posts, where the originator seems to have a female name, receive more replies than the ones where the DIC name seems to be a male ?"

HEHE, people are trying to get laid man

http://fukung.net/v/7465/unix-1.jpg

### #9 veronica 2707

Reputation: 0
• Posts: 19
• Joined: 17-March 09

## Re: Java ^2

Posted 18 March 2009 - 07:25 PM

Daegan, on 18 Mar, 2009 - 05:53 PM, said:

You aren't multiplying the numbers together.

You should use a number to track the 1-5 while loop, and another int variable to do the multiplication.

your sqt = (x)^2 won't work.

Try adding these to your code. You should be able to figure out where
```int y = 1;

```

```y = x * x;

```

thank you it working but still i have a problem...its endind only with 16,it should end with 25..help me fix this please

```public class Loopy {
public static void main(String[] args) {
int x = 2;
int y = 1;
System.out.println("Display Square Numbers from 1 to 5");
while (x <= 5) {
System.out.print("--> " + y);
System.out.println(" <--");
y = x * x;
x = x + 1;

}
System.out.println("");
}
}

```

### #10 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: Java ^2

Posted 18 March 2009 - 07:30 PM

mostyfriedman, on 18 Mar, 2009 - 08:22 PM, said:

THAT...is amazing.

Edit @ veronica 2707: You have an ordering problem and an initialization problem.

```// x should start at 1
int x = 1, y = 1;

while (x <= 5) {
y = x * x;
x++;

System.out.print("--> " + y);
System.out.println(" <--");
```

This post has been edited by Locke: 18 March 2009 - 07:39 PM

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Java ^2

Posted 18 March 2009 - 07:36 PM

Bacsue you are printing before adding 1 to x so you loose the last one

```public class Loopy {
public static void main(String[] args) {
int x = 1;	 // instead of 2
/// int y = 1;  useless you calculate it anyway
System.out.println("Display Square Numbers from 1 to 5");
while (x <= 5) {
int y = x * x;  // here a declare y
x = x + 1;
System.out.println("--> " + y + " <---");	 // a single println will do

}
System.out.println("");
}
}

```

This post has been edited by pbl: 18 March 2009 - 07:38 PM

### #12 veronica 2707

Reputation: 0
• Posts: 19
• Joined: 17-March 09

## Re: Java ^2

Posted 18 March 2009 - 07:40 PM

Locke, on 18 Mar, 2009 - 06:30 PM, said:

mostyfriedman, on 18 Mar, 2009 - 08:22 PM, said:

THAT...is amazing.

but Sir,
i have to display the square root numbers 1,4,9,16,25
and 25 is not displaying..

pbl, on 18 Mar, 2009 - 06:36 PM, said:

Bacsue you are printing before adding 1 to x so you loose the last one

```public class Loopy {
public static void main(String[] args) {
int x = 1;	 // instead of 2
/// int y = 1;  useless you calculate it anyway
System.out.println("Display Square Numbers from 1 to 5");
while (x <= 5) {
int y = x * x;  // here a declare y
x = x + 1;
System.out.println("--> " + y + " <---");	 // a single println will do

}
System.out.println("");
}
}

```

THANK YOU SOOOOOO MUCHHH SIR....I LOVE YOU!!!!!....

### #13 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: Java ^2

Posted 18 March 2009 - 07:40 PM

Yeah, sorry. I edited my post, see above. Sorry I took so long to edit, I had a few post problems.

### #14 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Java ^2

Posted 18 March 2009 - 07:46 PM

Final cleaned up version
Logically the x++ should be after the print of y

```public class Loopy {
public static void main(String[] args) {
int x = 1;
int to = 5;
System.out.println("Display Square Numbers from 1 to " + to);
while (x <= to) {
int y = x * x;
System.out.println("--> " + y + " <---");
x++;
}
}
}

```

This post has been edited by pbl: 18 March 2009 - 07:47 PM

### #15 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: Java ^2

Posted 18 March 2009 - 07:49 PM

@pbl: Why does it matter? It still acts the same way.

I guess it's more of a preference just to increment the variable at the end of the loop.

This post has been edited by Locke: 18 March 2009 - 08:03 PM