Multiplication table (need guide)

This is a discussion on Multiplication table (need guide) within the C Programming forums, part of the General Programming Boards category; hey there.. i kinda new for programmin.. i need guide from u guys to help me to solve multiplication table ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38

    Question Multiplication table (need guide)

    hey there..
    i kinda new for programmin..
    i need guide from u guys to help me to solve multiplication
    table program...
    Please teach me..

    Question: Write a program that accepts an integer in between 1 and 20 only (1 to 20) from the
    user then produces the multiplication table as shown below;
    Explanation: The number that the user entered is the starting number, and it will be incremented
    by 1 for three times such that the body of the multiplication table has 4 rows and
    4 columns, as shown below. You should use loop to create the body of the
    multiplication table

    OUTPUT:

    Enter a number (1 to 20): 0
    Enter a number (1 to 20):22
    Enter a number (1 to 20): 2

    ****:****2****3*****4******5 --> \*ignore ****: just wanna add tab, \t there.. *\
    ---------------------------------
    2**:*****4****6*****8*****10
    3**:*****6****9****12*****15
    4**:*****8***12****16****20
    5**:****10***15****20****25
    Last edited by naspek; 07-22-2009 at 11:55 PM.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Think about it for a while.
    This program is pretty simple.
    All you have to do is count 4 numbers up from the input and show the multiplication for the first elements for each one of them.
    Write some code and see where you get, after that ask for help if your stuck.
    Start with the easiest part which you are sure how to do, and then tackle the more advanced stuff.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by Spidey View Post
    Think about it for a while.
    This program is pretty simple.
    All you have to do is count 4 numbers up from the input and show the multiplication for the first elements for each one of them.
    Write some code and see where you get, after that ask for help if your stuck.
    Start with the easiest part which you are sure how to do, and then tackle the more advanced stuff.
    alright.. i'll try the easiest part first.. =)
    thanks spidey =)

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    should i insert "while" to control the user input?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    >> should i insert "while" to control the user input?

    I wouldn't worry about that part just yet. Focus on getting a program up and running with hard-coded values. Once that's running properly you can expand it to take user input.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by Sebastiani View Post
    >> should i insert "while" to control the user input?

    I wouldn't worry about that part just yet. Focus on getting a program up and running with hard-coded values. Once that's running properly you can expand it to take user input.
    i got stuck here.. :x

    Code:
    #include <stdio.h>
    
    int main()
    {
       int i, j, x;
       printf("Enter a number between 1 to 20 (1-20): ");
       scanf("%d", &x);
    
       for ( i = x; i <= x + 4; i++ )  {
          for ( j = x; j <= x+3; j++ )
             printf( "\t%d", i*(i+1) );
             printf( "\t%d", j*i );
          printf( "\n" );
       }
    
       return 0;
    }
    
    output:
    Enter a number between 1 to 20 (1-20): 5
            30      30      30      30      45
            42      42      42      42      54
            56      56      56      56      63
            72      72      72      72      72
            90      90      90      90      81
    can sumone consult me.. plz~ :sweat:

  7. #7
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Well, your almost there.

    However, what is the purpose of this line -

    Code:
    printf( "\t%d", i*(i+1) );
    also, your for loop is missing parentheses, so only the first line will execute and skip the rest. Hence j will only show the value which it received at the end of the for loop.

    Code:
    for ( i = x; i <= x + 4; i++ )  {
          for ( j = x; j <= x+3; j++ )
             printf( "\t%d", i*(i+1) );
             printf( "\t%d", j*i );
          printf( "\n" );
       }
    Think about which lines are necessary in the for loop and which ones are not, and then restructure your loop.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Code:
       for ( i = x; i <= x + 3; i++ )  {
          for ( j = x; j <= x + 3; j++ )
             // print current product of i and j
          printf( "\n" );
       }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  9. #9
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by Spidey View Post
    Well, your almost there.

    also, your for loop is missing parentheses, so only the first line will execute and skip the rest. Hence j will only show the value which it received at the end of the for loop.
    ok... so.. is my new for loop correct now?
    Code:
       for ( i = x; i <= x + 3; i++ )  {
          for ( j = x; j <= x+3; j++ ) {
             printf( "\t%d", i++);
             }
             printf( "\t%d", x*(j++));
          printf( "\n" );
       }
    addin the parentheses right?

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    >> ok... so.. is my new for loop correct now?

    Why are you incrementing the values of 'i' and 'j' within the print statement? Also, why print the value of 'i' at all? And why multiply 'j' with 'x'? Think about it. You *just* need the product of 'i' and 'j' from within the innermost loop!
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  11. #11
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by Sebastiani View Post
    >> ok... so.. is my new for loop correct now?

    Why are you incrementing the values of 'i' and 'j' within the print statement? Also, why print the value of 'i' at all? And why multiply 'j' with 'x'? Think about it. You *just* need the product of 'i' and 'j' from within the innermost loop!
    for ur question: i thought i can get my multiplication table.. but, i'm wrong..
    ok.. i'd change my for loop..
    but the output kinda weird..

    Code:
    for ( i = x; i <= x + 3; i++ )  {
          for ( j = x; j <= x+3; j++ ) {
             printf( "\t%d", j);
             printf( "\t%d", i);
             }
          printf( "\n" );
       }
    OUTPUT
    Enter a number between 1 to 20 (1-20): 5
            5       5       6       5       7       5       8       5
            5       6       6       6       7       6       8       6
            5       7       6       7       7       7       8       7
            5       8       6       8       7       8       8       8

  12. #12
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Now you are just displaying the values of i and j without multiplying anything.
    Like Sebastiani said, heres what you have to do -

    Quote Originally Posted by Sebastiani View Post
    Code:
       for ( i = x; i <= x + 3; i++ )  {
          for ( j = x; j <= x + 3; j++ )
             // print current product of i and j
          printf( "\n" );
       }
    Think about it.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    >> for ur question: i thought i can get my multiplication table.. but, i'm wrong..

    Ok, so now remove one print statement (either will do) and run the program. Then comment out that one and uncomment the other and run the program. Do you see how the values of 'i' and 'j' change? Now you just need to calculate the product of the two. That's all you need!
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  14. #14
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    ok.. i try print it.. but.. the vertical line does not change..
    my loop is now correct aite?
    just the printf part is wrong aite?
    Code:
    for ( i = x; i <= x + 3; i++ )  {
          printf("\t%d", x);
          for ( j = 2; j <= 5; j++ )
            printf("\t%d", x*j);
          printf( "\n" );
    
    OUTPUT
    Enter a number between 1 to 20 (1-20): 5
            5       10      15      20      25
            5       10      15      20      25
            5       10      15      20      25
            5       10      15      20      25

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Maybe this will clear things up:

    Code:
    int main()
    {
       int i, j, x;
       printf("Enter a number between 1 to 20 (1-20): ");
       scanf("%d", &x);
    puts("------ printing values for 'j' ------");   
     for ( i = x; i <= x + 3; i++ )  {
          for ( j = x; j <= x+3; j++ ) {
             printf( "\tj: %d", j);
             //printf( "\ti: %d", i);
             }
          printf( "\n" );
       }
    puts("------ printing values for 'i' ------");   
    for ( i = x; i <= x + 3; i++ )  {
          for ( j = x; j <= x+3; j++ ) {
             //printf( "\tj: %d", j);
             printf( "\ti: %d", i);
             }
          printf( "\n" );
       }   
       return 0;
    }
    So you see, the inner loop only needs *one* print statement. Furthermore, it just needs to print the product of the two values at that point (notice how if you multiplied the same row/column of the two output tables you'd get the value you're looking for).
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiplication table
    By freddyvorhees in forum C++ Programming
    Replies: 6
    Last Post: 08-02-2008, 11:09 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. multiplication table
    By SpEkTrE in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 03:46 PM
  4. C++ Multiplication Table Generator
    By Visual Develope in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2002, 11:22 AM
  5. Multiplication Table Error
    By Okiesmokie in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2002, 02:31 PM

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