Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 19th, 2005, 8:15 PM   #1
raspberry
Newbie
 
Join Date: Mar 2005
Posts: 2
Rep Power: 0 raspberry is on a distinguished road
Help with develop a C++ program to display a multiplication table

anyone know how to write C++ code to display a multiplication table using two nested FOR loops to display the output shown below:
----------------------------------------------------------------------
Welcome to multiplication program, this program creates a
multiplication table for the number (1*1) through to (X*Y)
Please enter first limit number ==> 3
Please enter second limit number ==> 4

1*1=1
1*2=2
1*3=3
1*4=4
2*1=2
2*2=4
2*3=6
2*4=8
3*1=3
3*2=6
3*3=9
3*4=12
----------------------------------------------------------------------
raspberry is offline   Reply With Quote
Old Mar 19th, 2005, 10:43 PM   #2
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
think of it this way...

1*1 = 1
1*2 = 2
1*3 = 3

What do they all have in common? it's always 1 times something, until you get to whatever you want the next number to be. In your case it's 4, so the code would be...

assuming you know basic include functions, how to start off the main function, etc. (if you don't just ask me):

for (int i = 1; i <= 4; i++) // starting it off as 1*something, and continuous-
{ // it will increment i by 1 after j hits 4 and breaks
for (int j = 1; j <=4; j++) // the loop
{
cout << i << " * " << j;
}
}

return 0;
gardon is offline   Reply With Quote
Old Mar 19th, 2005, 10:45 PM   #3
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
#include <iostream>

int main(int argc, char *argv[]) {
    int min = 1;
    int max = 10;

    for(int i=min;i<=max;i++) {
        for(int j=min;j<=max;j++)
            cout (i*j) << "\t";
        cout << endl;
    }

    return 0;
}
__________________

tempest is offline   Reply With Quote
Old Mar 19th, 2005, 10:52 PM   #4
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
bvbvb

Last edited by gardon; Mar 19th, 2005 at 11:09 PM.
gardon is offline   Reply With Quote
Old Mar 19th, 2005, 10:58 PM   #5
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
jjjbv

Last edited by gardon; Mar 19th, 2005 at 11:09 PM.
gardon is offline   Reply With Quote
Old Mar 19th, 2005, 11:08 PM   #6
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
Woah that sucked: I realized I screwed up somewhere but couldn't find out why. Lol then i remembers a loop problem. Anyways, sorry it took so long, my bad dude:



#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
cout << "Enter two numbers to be used for the x and y values:\n";
int x, y;
cin >> x >> y;

for (int ymin = 1; ymin <= y; ymin++)
{
for (int xmin =1; xmin <= x; xmin++)
{
cout << (xmin * ymin) << '\t';
}
cout << "\n";
}
return 0;
}
gardon is offline   Reply With Quote
Old Mar 20th, 2005, 1:22 AM   #7
raspberry
Newbie
 
Join Date: Mar 2005
Posts: 2
Rep Power: 0 raspberry is on a distinguished road
thank you

I done it ... thank you for you help ... :p
raspberry is offline   Reply With Quote
Old Mar 20th, 2005, 11:18 AM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I couldn't help myself:

#include <stdio.h>

int main() {
	char input[4];
	int x = 0, y = 0;
	int max_x = 0, max_y = 0;
	int i = 0;
	
	while ((max_x < 1) || (max_x > 15)) {
		printf("Enter maxima #1: ");
		scanf("%3s", &input);
		max_x = atoi(input);
	}
	
	while ((max_y < 1) || (max_y > 15)) {
		printf("Enter maxima #2: ");
		scanf("%3s", &input);
		max_y = atoi(input);
	}
	
	for (y = 0; y <= max_y; y++) {
		for (x = 0; x <= max_x; x++) {
			if (y) {
				if (x) {
					printf("%4i", x * y);
					if (x == max_x) {
						printf("\n");
					}
				}
				else {
					printf("%2i |", y);
				}
			}
			else {
				if (x) {
					printf("%4i", x);
					if (x == max_x) {
						printf("\n");
						for (i = 0; i < (max_x + 1) * 4; i++) {
							printf("-");
						}
						printf("\n");
					}
				}
				else {
					printf("   |");
				}
			}
		}
	}
	
	fflush(stdin);
	getchar();
	return 0;
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC