#include <iostream> #include <iomanip> using namespace std; int main() { int startNumber; int endNumber; int colCounter = 0; int rowCounter = 0; cout << "Multiplication Table\n" << "Enter starting number: "; cin >> startNumber; cout << "Enter ending number: "; cin >> endNumber; cout << setw ( 4 ) << right << "|"; for ( int colNumber = startNumber; colNumber <= endNumber; ++colNumber ) cout << left << setw ( 5 ) << colNumber; cout << "\n------------------------------------------------------\n"; for ( int colNumber = startNumber; colNumber <= endNumber; ++colNumber ) { cout << setw ( 3 ) << left << colNumber << '|'; for ( int currentRow = startNumber; currentRow <= endNumber; ++currentRow ) cout << setw ( 5 ) << left << colNumber*currentRow; cout << "\n |\n"; colCounter = colCounter + 1; rowCounter = rowCounter + 1; } cout << "Rows printed: " << rowCounter << endl; cout << "Columns Printed: " << colCounter << endl; cout << "Sum: "; //<< theSum << endl; return 0; }

# Multiplication Table

## I am trying to creat a multiplication table that show the rows printed

Page 1 of 1## 9 Replies - 6031 Views - Last Post: 16 May 2009 - 01:07 PM

### #1

# Multiplication Table

Posted 15 May 2009 - 11:05 PM

I am trying to create a c++ console application that shows a multiplication table with the amount of rows printed, columns printed and the sum. This is my code so far and it shows the multiplication table, rows and columns printed. I can't figure out how to code the sum into all of this and have been at it for days now. PLEASE HELP!!!!

##
**Replies To:** Multiplication Table

### #2

## Re: Multiplication Table

Posted 15 May 2009 - 11:27 PM

What exactly are you looking for the sum of? All numbers in the table? Sum of each row/column?

### #3

## Re: Multiplication Table

Posted 16 May 2009 - 03:21 AM

### #4

## Re: Multiplication Table

Posted 16 May 2009 - 03:43 AM

Just keep a running total as you add them in then.

Declare and initialize a new variable somewhere before you get into your loops.

Then just add the numbers to it as you put them in the table.

Declare and initialize a new variable somewhere before you get into your loops.

int sum = 0;

Then just add the numbers to it as you put them in the table.

sum += colNumber*currentRow;

### #5

## Re: Multiplication Table

Posted 16 May 2009 - 03:48 AM

aceofspades686, on 16 May, 2009 - 02:43 AM, said:

Just keep a running total as you add them in then.

Declare and initialize a new variable somewhere before you get into your loops.

Then just add the numbers to it as you put them in the table.

Declare and initialize a new variable somewhere before you get into your loops.

int sum = 0;

Then just add the numbers to it as you put them in the table.

sum += colNumber*currentRow;

I put it in the code where currentRow is initialized and I am getting an error saying currentRow is undeclared.

### #6

## Re: Multiplication Table

Posted 16 May 2009 - 03:55 AM

for ( int currentRow = startNumber; currentRow <= endNumber; ++currentRow ) cout << setw ( 5 ) << left << colNumber*currentRow; sum += colNumber*currentRow; cout << "\n |\n"; colCounter = colCounter + 1; rowCounter = rowCounter + 1; }

Try putting it inside this loop As I said, you add the numbers to it as you add them to the table.

### #7

## Re: Multiplication Table

Posted 16 May 2009 - 04:31 AM

aceofspades686, on 16 May, 2009 - 02:55 AM, said:

for ( int currentRow = startNumber; currentRow <= endNumber; ++currentRow ) cout << setw ( 5 ) << left << colNumber*currentRow; sum += colNumber*currentRow; cout << "\n |\n"; colCounter = colCounter + 1; rowCounter = rowCounter + 1; }

Try putting it inside this loop As I said, you add the numbers to it as you add them to the table.

Maybe I'm too dumb at C++ to get it, but now my code is completely whacked it only prints 1 row with a whole bunch of numbers after it. The code comes out like this:

2 4 2 468103 6912154 8121620Rows Printed: 3

Columns Printed: 3

Sum: 9

This post has been edited by **Myro**: 16 May 2009 - 04:37 AM

### #8

## Re: Multiplication Table

Posted 16 May 2009 - 07:28 AM

try this one!!!

maybe this could help it out...........

tell me if you want any modifications....

maybe this could help it out...........

tell me if you want any modifications....

//A PROGRAM TO DISPLAY THE TABLE OF A NUMBER GIVEN BY USER #include<stdio.h> #include<conio.h> main() { int num,i; clrscr(); printf("Enter the Number\n\n\n\n"); scanf("%d",&num); for(i=1;i<=10;i++) printf("\n%d X %d=\t%d",num,i,num*i); getch(); }

This post has been edited by **pdkharkar**: 16 May 2009 - 07:29 AM

### #9

## Re: Multiplication Table

Posted 16 May 2009 - 09:08 AM

I already cover this solution in one of my blog entries. I show one way to do it, explain it and provide a screenshot of the results.

Martyr2's Programming Underground - Simple Multiplication Table in C++

Enjoy!

Martyr2's Programming Underground - Simple Multiplication Table in C++

Enjoy!

### #10

## Re: Multiplication Table

Posted 16 May 2009 - 01:07 PM

Well, Martyr's example is going to be about 10x better to follow, but I was bored this morning and decided to compile what you had to see the problem. Most of what was causing the issue was a lot of misplaced loops and logic errors when outputting the different cells (the big one being, you don't want a line break after each cell ).

Page 1 of 1