# Multiplication Table using "For" loops

Page 1 of 1

## 2 Replies - 46819 Views - Last Post: 23 March 2008 - 08:19 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=47020&amp;s=ffe304aa0bbb0478d08ca4777b62660f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jvoorhees

Reputation: 0
• Posts: 1
• Joined: 23-March 08

# Multiplication Table using "For" loops

Posted 23 March 2008 - 12:35 AM

I have an assignment to write a program that uses at least one for loop to print a section of the multiplication table. The user should be able to specify the section of the table to be displayed. The program will also display a count of the number of rows printed, the count of the columns printed and the sum of all calculated values.
The user input will appear as such:

**Create a Multiplication Table**
Enter the starting value: 2
Enter the ending value: 5

The output will be:
X 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Rows printed = 4
Columns printed = 4
Sum: 196

Here is what I have so far:

```#include <iostream>

using namespace std;

int main() {

// declare variables
int startNumber = 0;
int lastNumber = 0;
int columnCounter = 0;
int rowCounter = 0;
int accumulator = 0;
int currentRow = 1;

cout << "Create a Multiplication Table\n" << endl;

//The user enters the starting and ending values
cout << "Enter the starting value: ";
cin >> startNumber;
cout << endl;
cout << "Enter the ending value: ";
cin >> lastNumber;
cout << endl;

int rowNumber = lastNumber;

//display the table
cout << " " << " X ";  //display the 'X'

for (int i = startNumber; i <= lastNumber; i++ ){
cout << " " << i << " ";	// display the column headings
columnCounter = columnCounter + 1;
}
cout << endl;

//the table body and row headings
//outside loop
while (currentRow != rowNumber){
cout << "  " << (currentRow + 1) << "  "; // display row headings
// inside loop
for (int i = startNumber; i <= lastNumber; i++ )
{
cout << (i + i) << "  ";  // output products (row * col)
accumulator = accumulator + i;  // accumulate the product
}
currentRow +=1;
rowCounter = rowCounter + 1;
cout << endl; // go to the next row
}

cout << endl;

cout << "Rows printed = " << rowCounter << endl;  // output row number
cout << "Columns printed = " << columnCounter << endl;  //output column number
cout << "Sum: "<< accumulator << endl << endl;  // output sum

return 0;
}

```

Using the previous example, the output looks like this:

X 2 3 4 5
2 4 6 8 10
3 4 6 8 10
4 4 6 8 10
5 4 6 8 10

Rows printed = 4
Columns printed = 4
Sum: 56

Is This A Good Question/Topic? 0

## Replies To: Multiplication Table using "For" loops

### #2 MorphiusFaydal

• D.I.C Lover

Reputation: 43
• Posts: 1,376
• Joined: 12-May 05

## Re: Multiplication Table using "For" loops

Posted 23 March 2008 - 12:40 AM

A pair of for loops, one inside the other.

### #3 Sepanto

Reputation: 0
• Posts: 97
• Joined: 20-March 08

## Re: Multiplication Table using "For" loops

Posted 23 March 2008 - 08:19 AM

I didn't actually check the code so there may be some syntax errrors (; missing or somthing)
```#include <iostream>
using namespace std;
void mult(int x,int y)
{
int i,j;
cout<< "X";
for (i=x;i<=y;i++)
cout <<" "<<i;
cout << endl;
for (i=x;i<=y;i++){
cout<<i<<" ";
for (j=x;j<=y;j++)
cout<<i*j<<" ";
cout << "\n";
}
}
int main()
{
int x,y;
cin>>x>>y;
if (x<y)
mult(x,y);
else
mult (y,x);
return 0;
}

```

This post has been edited by Sepanto: 23 March 2008 - 08:20 AM