Thread: Enquiry on generation of number pattern using nested for loops

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Question Enquiry on generation of number pattern using nested for loops

    May I have your advice on the generation of the following number pattern using nested for loops? Thank you so much ~

    2 4 6
    7 9 11
    12 14 16
    17 19 21


    But I can only figure out

    2 4 6
    3 5 7
    4 6 8
    5 7 9

    and below is the code:

    Code:
    #include <stdio.h>
    int main() {
            int i, j;
    
            for (i=0; i<4; i++){
                    for (j=2; j<7; j=j+2){
                    printf("%d ", i+j);
            }
            printf("\n");
            }
            getchar();
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Why are you only incrementing i by one each time. How much does each number change from row to row?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that the first number in each row differs from the first number of the next row by 5. Notice that each number of each row differs from the next number in the same row by 2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    I got it. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-23-2009, 01:55 PM
  2. Replies: 5
    Last Post: 10-05-2009, 10:21 AM
  3. Working with Nested "Switch Multiple-Selection"
    By MAV_DevWantB in forum C Programming
    Replies: 5
    Last Post: 09-18-2009, 08:05 AM
  4. Desperate for help - ugly nested if
    By baseballkitten in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 03:56 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM