Thread: help making triangle

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    help making triangle

    so I am trying to make a triangle in which :

    1. The number of elements in a row is equal to the row number
    2. Each element is the product of the row number by the column number.

    Example Output:
    Enter a number: 7
    1
    2 4
    3 6 9
    4 8 12 16
    5 10 15 20 25
    6 12 18 24 30 36
    7 14 21 28 35 42 49

    However when I run the program all I get is:
    Enter a number: 7
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5
    6 6 6 6 6 6
    7 7 7 7 7 7 7


    PLEASE HELP!!! Thanks in advance

    Heres my code as of now:

    Code:
    #include <stdio.h>
    
    int main ()
    {
            int num, row, col;
    
            printf("Enter a number: ");
            scanf("%d", &num);
    
            for (col = 1; col <= num; col++) {
                    for (row = 1; row <= col; row++) {
                            col = col*row;
                            printf("%5d", col);
                    }                
                    printf("\n");
            }
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    use a different variable to hold col*row
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd change the order of the for loops: row first, then col. The value needed changes with every col, not just with every row.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    23
    thanks, that really did the work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 02:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  3. Replies: 14
    Last Post: 11-09-2005, 11:28 PM
  4. Stupid Logic Problem Need Outside Viewpoint
    By RP319 in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2005, 10:59 PM
  5. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM