![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 2
Rep Power: 0
![]() |
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 ---------------------------------------------------------------------- |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
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; |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
#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;
}
__________________
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
bvbvb
Last edited by gardon; Mar 19th, 2005 at 11:09 PM. |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
jjjbv
Last edited by gardon; Mar 19th, 2005 at 11:09 PM. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
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; } |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Mar 2005
Posts: 2
Rep Power: 0
![]() |
thank you
I done it ... thank you for you help ... :p
|
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|