nested loops

 Home

News

Forums

Hardware

CPUs

Motherboards

Video

Guides

CPU Prices

RAM Prices


Sharky Extreme : Forums:


Results 1 to 4 of 4

Thread: nested loops

  1. #1
    Catfish f15e's Avatar
    Join Date
    Mar 2002
    Location
    I live in the south.
    Posts
    114

    nested loops

    Here's what I need to do! I need to write a nested loop code segment that produces the following output:

    1
    1 2
    1 2 3
    1 2 3 4
    I know that this is elementary to most of you. I know how to make the program increment numbers:
    int i = 1;
    while(i < 5)
    {
    cout << i <<endl;
    i++;
    }

    but how do get it to go back to 1 and inrement to 2 then on the next line 1 2 3, etc. I know I need to nest the loops but I am fairly new to loops, I am a beginner, and not sure how to make it produce the output listed above. I am not asking anyone to do this program for me only asking for some hints and direction. THANK YOU!!!!

  2. #2
    Tiger Shark DeadKen's Avatar
    Join Date
    Oct 2001
    Location
    Seattle
    Posts
    800

    Re: nested loops

    Originally posted by f15e
    Here's what I need to do! I need to write a nested loop code segment that produces the following output:

    1
    1 2
    1 2 3
    1 2 3 4
    I know that this is elementary to most of you. I know how to make the program increment numbers:
    int i = 1;
    while(i < 5)
    {
    cout << i <<endl;
    i++;
    }

    but how do get it to go back to 1 and inrement to 2 then on the next line 1 2 3, etc. I know I need to nest the loops but I am fairly new to loops, I am a beginner, and not sure how to make it produce the output listed above. I am not asking anyone to do this program for me only asking for some hints and direction. THANK YOU!!!!

    int j = 1;

    while (j<5)
    {
    int i = 1;
    while(i < j)
    {
    cout << i << " ";
    i++;
    }


    cout << i << endl;
    }
    I want an OS, not a hobby...

    Theres nothing more pathetic then someone who wears non-matching socks on purpose.

  3. #3
    Catfish f15e's Avatar
    Join Date
    Mar 2002
    Location
    I live in the south.
    Posts
    114

    DeadKen

    THANKS DeadKen!!!

    I appreciate your help! I needed to add a j++ to the end of the code and it worked. THANKS AGAIN!!!

  4. #4
    Sushi
    Join Date
    Feb 2012
    Posts
    1
    How's about Java codes for producing the following:
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3
    1 2
    1
    Thanks !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •