Java ^2

squaring

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1054 Views - Last Post: 18 March 2009 - 08:00 PM Rate Topic: -----

#1 veronica 2707  Icon User is offline

  • New D.I.C Head

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

Java ^2

Post icon  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
help me please

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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 Daegan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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 ?" :D
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: Java ^2

Posted 18 March 2009 - 07:22 PM

View Postpbl, 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 ?" :D


HEHE, people are trying to get laid man :P

http://fukung.net/v/7465/unix-1.jpg
Was This Post Helpful? 0
  • +
  • -

#9 veronica 2707  Icon User is offline

  • New D.I.C Head

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

Re: Java ^2

Posted 18 March 2009 - 07:25 PM

View PostDaegan, 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("");
   }
}


Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Java ^2

Posted 18 March 2009 - 07:30 PM

View Postmostyfriedman, 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

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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
change a bit your logic

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

Was This Post Helpful? 0
  • +
  • -

#12 veronica 2707  Icon User is offline

  • New D.I.C Head

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

Re: Java ^2

Posted 18 March 2009 - 07:40 PM

View PostLocke, on 18 Mar, 2009 - 06:30 PM, said:

View Postmostyfriedman, 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..
please help me fix this

View Postpbl, on 18 Mar, 2009 - 06:36 PM, said:

Bacsue you are printing before adding 1 to x so you loose the last one
change a bit your logic

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!!!!!....
Was This Post Helpful? 0
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • 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.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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

Was This Post Helpful? 0
  • +
  • -

#15 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • 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. :rolleyes:

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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2