Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: My first code for computing a table of squares - and its not working :(

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My first code for computing a table of squares - and its not working :(

    public class squares {
     
    	public static void main(String[] args) {
    		int i =1, j=1;
    		System.out.println (i);
    		while (j < 40)
    		{
    			i=i+1;
    			j=i*i;
    			System.out.println(j);
    		}   
     
    	}
     
    }


    The output prints the squares till 49 even though i have given j as 40..please help!
    Last edited by jps; August 24th, 2013 at 11:13 AM. Reason: code tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My first code for computing a table of squares - and its not working :(

    Please post your code in code tags.

    Changing the value of the loop control variable, in this case 'j', inside the loop is not a great idea. I recommend that you use another variable to compute the squares and print that, leaving 'j' alone.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My first code for computing a table of squares - and its not working :(

    Thanks Greg
    Will try that..by the way changing the order of print ( printing at the start of the loop helped too )

  4. #4
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: My first code for computing a table of squares - and its not working :(

    I have an explaination for your code.
    Your code makes sqaures indeed but what happens is that when the last square is made(i = 7 ; 7 * 7= 49) the loop will stop because j is over 40.
    If you want to stop the loop before it goes over 40 then you should use the square before 40 that is 36(6 * 6).
    Then you should test what the sqaure is going to be before it is going to print it.

  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: My first code for computing a table of squares - and its not working :(

    Hello.
    If you have time then using a pen and paper just trace your program line by line.
    You shall know what is causing the fault. And you may be able to fix it on your own.

    Syed.

Similar Threads

  1. Code to read a table from different applications
    By olabanjitajudeen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 22nd, 2013, 07:47 AM
  2. how to code dynamic updation of table fields??
    By abhilasha8055 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 23rd, 2013, 09:37 AM
  3. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  4. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  5. Collision Detection Between Two Squares
    By thegreatzo in forum Loops & Control Statements
    Replies: 7
    Last Post: August 22nd, 2012, 09:13 AM