For loop with multiplication table. not iterating properly

i want to write a multiplication table that out put in the following format
1
2
3
4
1 x 1 = 1
1 x 2 = 2
...
9 x 9 = 81


below is what i have written so far but its giving me errors, if i declare the variables outside the loop (i think that's what am supposed to do) it compiles but gives me wrong result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;


int main () {

int i=1;  //so that second for loop can access it.
   
    for(i ;i<12;i++);
    {  
        for(int n =1;n<=12;n++)
        {
            
       int result= i*n; 
       
         cout <<i<<"*"<<n<<"=" <<result<< '\n';  
        
        }                   
    } 
    
}
    




thanks.


Last edited on
Remove the semicolon at the end of line 10.
for(i ;i<12;i++);

Remove the semicolon.

This means the code in the braces will only execute once.

This is a compile error in my system.

One thing I do is to format the for loop braces like this:

1
2
3
4
5
6
for(i ;i<12;i++)   
        for(int n =1;n<=12;n++)  {
              int result= i*n; 
              cout <<i<<"*"<<n<<"=" <<result<< '\n';  
        }                   
}


If you put the opening brace on the same line, you won't be tempted to put a semicolon there.

If you have nested loops, you just have to remember not to put semicolons.
Last edited on
thanks very much Peter87 .& TheIdeasMan. that was so stupid of me not to realise :-)
Topic archived. No new replies allowed.