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 7 of 7

Thread: nested for loop

  1. #1
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default nested for loop

    Using nested for loops, write a program that will print the numerals 1 through 9 a number of times equal to its square. 1 will print one time (1^2), 2 will print 4 times (2^2), 3 will print 9 times (3^2), etc.

    Your output should look like this:

    1
    2222
    333333333
    4444444444444444
    5555555555555555555555555
    666666666666666666666666666666666666
    7777777777777777777777777777777777777777777777777
    88888888888888888888888888888888888888888888888888 88888888888888
    99999999999999999999999999999999999999999999999999 9999999999999999999999999999999

    ( I don't even know how to start or what the main idea is)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,079
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: nested for loop

    Here's the main idea: "Using nested for loops." Can you write a for loop? Try writing one or two to get this program going.

  3. #3
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: nested for loop

    I know how to use for loops but I don't know how to get it to do it the times equal to its square

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,079
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: nested for loop

    Write the for loop. Think about where it starts ( 1, 2, 3, 4, 5, . . . ) - pretty standard - and the upper limit of each starting point. I can't believe you can't get the basic structure of the for loop if you just try.

  5. #5
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: nested for loop

    I get what you are saying
    for(number =1; number <= 9; number++)

    the thing I don't get is the actual spacing and the numbers

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,079
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: nested for loop

    So one for loop, the outer loop, counts the variable 'number' from 1 through 9. That's useful. What could the inner loop do? Perhaps it could take the 'number,' square it, and print it some number of times. How many times?

    Write the inner for loop, then write the rest of the class that goes around the loops and test it.

  7. #7
    Junior Member yuli's Avatar
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: nested for loop

    ohh ok ill try it like that thank you ..I will see if it works

Similar Threads

  1. nested while loop
    By Amruta N in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 2nd, 2013, 12:35 AM
  2. nested for loop
    By tardis_ in forum Loops & Control Statements
    Replies: 4
    Last Post: April 18th, 2013, 01:11 PM
  3. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 20th, 2012, 02:49 PM
  4. Nested For Loop!
    By samadniz in forum Object Oriented Programming
    Replies: 3
    Last Post: September 3rd, 2012, 04:03 PM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM