Need help with a loop to make a multiplication table.

This is a discussion on Need help with a loop to make a multiplication table. within the C Programming forums, part of the General Programming Boards category; Problem: Write C loop to display the multiplication table as shown below. 1 2 3 4 5 6 7 8 ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    17

    Need help with a loop to make a multiplication table.

    Problem:
    Write C loop to display the multiplication table as shown below.
    1 2 3 4 5 6 7 8 9
    1 1
    2 2 4
    3 3 6 9
    4 4 8 12 16
    5 5 10 15 20 25
    6 6 12 18 24 30 36
    7 7 14 21 28 35 42 49
    8 8 16 24 32 40 48 56 64
    9 9 18 27 36 45 54 63 72 81

    It didn't line up exactly right(ignore the top row of it I got it working) but this is what I need to do, this is what I have:
    Code:
    
    #include <stdio.h>
    
    
    int main(void)
    {
        
        int col, row, sum, n=1;
    
    
        printf("This program diplay a multiplication table up to 9.\n");
    
    
        for (col = 1; col <= 1; col++)
        {
            for(row = 1; row <= 9; row++)
            {
                printf("\t%d", n++);
            }
            printf("\n");
        }
    
    
        getchar();
        getchar();
        return 0;
    
    
    }
    I'm thinking this will be done with more loops, and as you can see I already have the top row. I've tried many things to do this but this is the only thing I've gotten to work correctly. I need to know how to make the first column, and then how to make the part with all the answers. This is the most complicated program I've done yet and according to all my failed attempts, more than I know how to do. Help would be very much appreciated. Examples are preferred, it makes it much easier to understand what y'all are talking about.
    Last edited by jp5a9852; 10-31-2012 at 09:01 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Since the largest number will be 81, you can profitably use the %2d printf() format specifier - but make the %2 into %5, and you can remove the \t - which will work here, but is not as flexible as using %N (where N is any field width you need).

    (Never print on the 80th column of a text monitor however - the automatic newline will goof up your alignment on the columns)

    Any problem with rows and columns, is a great candidate for a nested pair of for loops.

    First for loop will handle the row logic your program needs.
    Second for loop will handle the columns being printed on any one row.

    Code:
    print the header across the top - using the field width specifier here, as well
    
    for(each row) { //row=0;row<9;row++
       for(each col) {  //col=0;col<9;col++
           //logic for the columns in any row
           //print the column using %N field width specifier
       }//done with the columns
       print a newline char here
    }down with a row
    Give that a shot!

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    Okay that helps a little, this is what I got now:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    
    
    int main(void)
    {
        
        int col, row, sum, n=1;
    
    
        printf("This program diplay a multiplication table up to 9.\n");
    
    
        for (row = 1; row <= 1; row++)
        {
            for(col = 1; col <= 9; col++)
            {
                printf("%5d", n++);
            }
            printf("\n\n");
        }
    
    
        n = 1;
    
    
        for (row = 1; row <= 9; row++)
        {
            for (col = 1; col <= 1; col++)
            {
                printf("%d", n++);
            }
            printf("\n\n");
        }
    I'm not sure I understood what you said, but I got the first column to work. So, the factors that are to be multiplied are displayed correctly, now how do I make a loop to display the answers in the right column and row. Also, is there anyway to do the entire program in one loop? Keep in mind what my main question asked, I have to do in a loop. I say that because I have a feeling that there might be more advanced ways to make something like this. Thanks for the previous and future help by the way.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your second pair of nested for loops - I don't understand why you need them. You have a header row, which you are not currently printing - why I have no friggin idea. Then you have the table below it. There is nothing else in your example to be printed out.

    You can't (and shouldn't be even if you can), print a header row inside nested for loops, and you can't/shouldn't, print a header row, after printing the body of the data out.

    The header row needs only ONE for loop - or none at all: print " 1 2 3 4 5 ...\n".

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    A loop from 1 to 1 isn't exactly a loop!

    How about you forget about the row and column headers, and forget about only doing the lower triangle of the table, and just do the whole N x N table.
    That much at least should be dead easy.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Solving this loop problem (Multiplication table)
    By everyone0 in forum C Programming
    Replies: 2
    Last Post: 05-05-2010, 11:44 AM
  2. Help Solving this loop problem (Multiplication table)
    By everyone0 in forum C Programming
    Replies: 0
    Last Post: 05-05-2010, 11:42 AM
  3. multiplication table without for loop
    By agomera in forum C Programming
    Replies: 3
    Last Post: 03-17-2008, 05:30 AM
  4. multiplication table
    By SpEkTrE in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 03:46 PM
  5. How to make a multiplication table up to 10*10=100?
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2002, 09:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21