Multiplication Table using loops

This is a discussion on Multiplication Table using loops within the C Programming forums, part of the General Programming Boards category; Hi there, I need some help with formatting on my program. I can't seem to get the table to look ...

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

    Multiplication Table using loops

    Hi there,

    I need some help with formatting on my program. I can't seem to get the table to look like this:



    Code:
    
          1   2   3   4
    -------------------
    1     1   2   3   4
    2     2   4   6   8
    3     3   6   9  12
    4     4   8  12  16
    Here is my code:

    Code:
    #include <stdio.h>
    
    
    int mult(int x, int y);
    
    
    int main(void)
    
    
    {
      int x, y;
    
    
    
    
      for (x = 1; x < 10; x++)  {
           printf("%4d", x);
        }
      printf("\n");
      printf("-------------------------------------\n");
    
    
      for (y = 1; y < 10; y++)  {
          printf("%d\n", y);
        }
    
    
      for (x = 1; x < 10; x++)   {
         for (y = 1; y < 10; y++)    {
           printf("%4d", mult(x, y));
           }
        }
      return (0);
    }
    
    
    int mult(int x, int y)
    {
     return(x * y);
    }
    Now, I realize this would be 100x easier with arrays, but we haven't learned them in class yet, so I need to use loops. I just don't know how to get the numbers I need on the lines I want, like shown above.

    Help/Tips appreciated.
    Last edited by lolcats22; 10-22-2011 at 08:27 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Make your "%d" fields bigger... maybe 6 instead of 4... then put a printf("\n"); at line 32... moving the brace to 33.
    Then all you need to do is adjust your header spacing at line 17 to match...

    And in this case... arrays would be overkill, and present you with the same formatting problems.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Code:
     for (y = 1; y < 10; y++)  {
          printf("%d\n", y);
        }
     
     
      for (x = 1; x < 10; x++)   {
         for (y = 1; y < 10; y++)    {
           printf("%4d", mult(x, y));
           }
        }
    Well at this part of the code in your top loop you are printing a single number followed by a new line. When that's complete you are then printing your multiplication table, so you must combine them in one loop.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @lolcats... please don't back edit your first message. It removes the context from the discussion below it.

    If you make changes, post your code again in thread order please.

    Also... note that you cannot edit messages more than 1 hour old. The software here prevents it.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Thank you both, I was able to fix my problems and get the table set up correctly.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    the editing was not done before any messages were made, nor was it done to the actual code. it was merely to show that I needed a table that looks like the one in the first code box, since regular text wasn't showing it properly.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries... Some forums encourage you to update the first message, we don't... I was just making sure you knew.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplication table
    By yatesj05 in forum C Programming
    Replies: 7
    Last Post: 04-17-2011, 02:46 AM
  2. C Multiplication Table
    By trueman1991 in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 06:58 AM
  3. Multiplication table
    By freddyvorhees in forum C++ Programming
    Replies: 6
    Last Post: 08-02-2008, 11:09 AM
  4. multiplication table
    By SpEkTrE in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 03:46 PM
  5. multiplication table help
    By cprogstudent in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:32 PM

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