1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Nested for loops

Discussion in 'C' started by donalt, May 25, 2010.

  1. donalt

    donalt New Member

    Joined:
    May 25, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi,

    iam trying to write a program using nested for loops which will display the following
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
     
  2. bluecoder

    bluecoder New Member

    Joined:
    Mar 11, 2010
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    what have you done so far ?
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    This is very easy. Your subject line contains the key. A nested for loop is simply one loop inside another, e.g.:
    Code:
    for (int i=0; i<10; i++)
    {
        for (int j=0; j<10; j++)
        {
            printf("%d %d\n",i,j);
        }
    }
    
    Start with a for loop that displays any number N of *'s (hint: a printf that displays one star inside a loop will do it). Change the value of N and check the program prints the correct number of *'s.

    Then put that for loop into another for loop and vary N from 1 to the relevant maximum.

    It's as simple as that!
     

Share This Page