For loop multiplication table.

Really stuck on this tbh, everything i've found on the internet is basically using things like setw which apparently we cant use and don't need to. Havn't learnt that yet anyways.

Basically the program asks what multiplication table they want to see, how many rows it should have. For example the 2 times table with 5 rows:

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10


This is what i've got so far and it's v.wrong =(

Thanx for any help !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream.h>
#include <windows.h>
#include <ctype.h>
int main()
{
    
    char selection;
    int number;
    int lines;
    
    cout << "Do you want Do you want to enter a [M]ultiplication table or would you like to"
    "[E]xit? Enter [M] or [E] only: " <<endl;
    cin >> selection;
    
    system("CLS");
    
    
    switch (selection)
           {
                      
                      case 'M':
                      case 'm': cout << "Please enter a number between 1 and 20 to find the multiplication table of: "<<endl;
                                 cin >> number;
                                 system ("CLS");
                                 cout << "How many lines would you like the table to display? Enter a number between 1 and 15: "<<endl;
                                 cin >> lines;
                                 
                                 for (count = 1; ; count++)
                                 { cout << "Multiplication table for " << number << endl;
                                     
                                     cout << number * count << 
                                  
                                  
                                  }
                                 break;
                                 
                      case 'E':
                      case 'e': cout << "Goodbye";
                      break;
                      
                      default : cout << "Please enter a valid number.";
           }
        
    
    
    Sleep (10000);
    return 0;
}
I just tried your problem and what I did was -

* Prompt user for 2 variables -
1. user_number (which number to show multiplication table for) &
2. row_number (how many rows to show multiplication table)
* Use a for() loop to do something like :
1
2
3
4
for(int i = 1; i <= row_number; i++) //i keeps count of current row
{
      //print the result here
}


Using this method you can handle larger numbers (more than 20 and more than 15 rows).
Last edited on
My mates figured it out using a DO/While loop I believe, I don't need to handle more numbers the assignment is only 20 and 15 I believe, thanks for the help will try it mate :)
If anyone can figure out how to do this using a Do/While loop it'd be greatly appreciated!!
A for is better for this kind of things but you can easily use a do-while
Here I'm using the same notation bluezor used but with the do-while:
1
2
3
4
5
int i = 1;
do
{
    // print the result here
} while ( ++i <= row_number );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream.h>
#include <windows.h>
#include <ctype.h>
int main()
{
    
    char selection;
    int number;
    int lines;
    int count;
    
    cout << "Do you want Do you want to enter a [M]ultiplication table or would you like to"
    "[E]xit? Enter [M] or [E] only: " <<endl;
    cin >> selection;
    
    system("CLS");
    
    
    switch (selection)
           {
                      
                      case 'M':
                      case 'm': cout << "Please enter a number between 1 and 20 to find the multiplication table of: "<<endl;
                                 cin >> number;
                                 system ("CLS");
                                 cout << "How many lines would you like the table to display? Enter a number between 1 and 15: "<<endl;
                                 cin >> lines;
                                 system("CLS");
                                 
                                 
                                 
                                 for (count = 1; count <= lines ; count++)
                                 { 
                                     cout << "This is the multiplication table for the " << number << " times table";
                                     cout<<endl;                   
                                     cout << count << " * " << number<< " = " << count*number<<endl;
                                  
                                  }
                                 break;
                                 
                                 
                      
                      
                      case 'E':
                      case 'e': cout << "Goodbye";
                      break;
                      
                      default : cout << "Please enter a valid number.";
           }
        
                         
    
    system("PAUSE");
    return 0;
}




I've got it all working, the only thing I need to do now is include an if statement to check if "numbers" and "lines" are valid ie: "numbers" is <= 20 and lines is <=15, I tried to put an if statement in but it didn't work, I also need to make it loop after 10seconds displaying the table. Can I use an if statement inside a switch statement? Or is there a way to use the switch to do what I want?
nice one got the if tags working I was putting them in the wrong place !!! Just need to sort out a loop back to the beginning program now!! Sorry for the double post
Topic archived. No new replies allowed.