Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: for

  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Malaysia
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    for

    Dear sir,

    I use for loop to produce the following output, but my second for can't be functioned properly .

    Proper output:
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    1 2 3 4 5 6
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7
    1 2 3 4 5 6
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1

    #include <stdio.h>
    #include <conio.h>

    void main(void)
    {
    int a;
    int b;

    clrscr();

    for (a = 1; a <= 9; a++)
    { for (b = 1; b <= a; b++)
    printf("%i\t", b);
    printf("\n");
    }


    for (a = 1; a <= 8; a++)
    { for (b = 8; b >= a; b--)
    printf("%i\t", b);
    printf("\n");
    }

    getch();
    }


    Thanks


    Xiang

  • #2
    DaZ
    DaZ is offline
    New Coder
    Join Date
    Dec 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Xiang

    for (a = 1; a <= 8; a++)
    { for (b = 8; b >= a; b--)
    printf("%i\t", b);
    printf("\n");
    }
    try this for your second for statement
    -----------------------------------------------

    ive just reversed the numbers that it prints out.


    for (a = 8; a >= 1; a--)
    { for (b = 1; b <= a; b++)
    printf("%i\t", b);
    printf("\n");
    }

    see if thats ok


  •  

    Posting Permissions

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