Thread: How to you loop this?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    19

    How to you loop this?

    1
    12
    123
    1234
    12345
    1234
    123
    12
    1

    or

    1
    12
    123
    1234
    12345
    123456
    1234567
    123456789


    can some1 found me some example so i can have some idea..
    Last edited by Aznmask; 11-13-2002 at 06:20 PM.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I did the first example using two for loops. However it come out
    123454321 and not

    1
    2
    3
    4
    5
    4
    3
    2
    1 but you can just format that.

    Code:
    #include <iostream>
    #include <conio.h> // for getch
    
    using namespace std;
    
    int main ()
    {
        int i;
    
        for (i = 1; i<= 5; i++) // loop to get 12345
        {
        cout <<i;
        }
        for (i = 4; i>=1; i--) // loop to get 4321
        {
        cout <<i;
        }
        getch(); //ignore this for my compiler
        return 0;
    }
    IM just not exactly sure how to get 1
    12
    123
    But this is a start i guess

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Aznmask,

    Run a search on creating triangles. Your question is nearly as common as the "clear the screen" question.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Here a small example using printf:
    Code:
    #include <string.h>
    
    int main(void)
    {
       int i;
       char msg[] = "12345";
       int l = strlen(msg);
    
       for(i = 1; i < l; i++) printf("%.*s\n", i, msg);
       for(i = l; i > 0; i--) printf("%.*s\n", i, msg);
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    hmm..

    hmm.. i know how to get the first half of the first problem

    Code:
    for (a = 2; a < 7; a++)
      {
         for (x = 1; x < a; x++)
          {
            cout << x;
          }
        cout << endl;
      }
    i know there is a way to write the entire problem 1 in nested loops.. i just did it not but a week ago.. but it seems to have fled my brain.. bummer...

    this set of code produces the following output
    Code:
    1
    12
    123
    1234
    12345

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    Originally posted by Monster
    Here a small example using printf:
    Code:
    #include <string.h>
    
    int main(void)
    {
       int i;
       char msg[] = "12345";
       int l = strlen(msg);
    
       for(i = 1; i < l; i++) printf("%.*s\n", i, msg);
       for(i = l; i > 0; i--) printf("%.*s\n", i, msg);
       return 0;
    }

    THANK-YOU for everyone's reply...

    Hey Monster my teacher never teach us printf. Is there other command that is equal to printf? I think printf is Java's command but we learning C++
    Last edited by Aznmask; 11-11-2002 at 01:09 PM.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    printf() is valid in C as well as C++. (here is no cout in C.) Check out sprintf() too.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    PLEASE HELP I'm not asking your guys to do my work. BUt atleast give me some hint some instruction to do those while loops

    thz

  9. #9
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    Originally posted by Aznmask
    PLEASE HELP I'm not asking your guys to do my work. BUt atleast give me some hint some instruction to do those while loops

    thz

    Here is an example of the while

    while (statement) // if this is true perform statement_1 if false skip it
    {
    statement_1;
    }

    i hope this helped
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM