Solve this in c++

Status
Not open for further replies.

perk_bud

Journeyman
Hi friend's i'm using Turbo c I need a solution for this problem

How to create multiplication tables for numbers any range(i'm taking tables from 2 to 5) and arrange them like this(fromat is important)

2 | 3 | 4| 5
4 | 6 | 8| 10
6 | 9 |12| 15
8 |12|16| 20
10|15|20| 25
12|18|24| 30
14|21|28| 35
16|24|32| 40
18|27|36| 45
20|30|40| 50
 

QwertyManiac

Commander in Chief
Are you sure about your entered formatting here?

The spaces are uneven for the last column and also for first 4 rows. Perhaps you tried to make them look even?

If so, in left-alignment:
Code:
#include<iostream>

using namespace std;

int main()
{
	int multiplier[]={2,3,4,5};
	int i, j;
	
	cout.setf(ios::left);
	
	for(i=1;i<11;i++)
	{
		for(j=0;j<4;j++)
		{
			cout.width(2);
			cout<<multiplier[j]*i;
			cout<<"|";
		}
		
		cout<<endl;
	}
	
	return 0;
}

Outputs as:
Code:
2 |3 |4 |5 |
4 |6 |8 |10|
6 |9 |12|15|
8 |12|16|20|
10|15|20|25|
12|18|24|30|
14|21|28|35|
16|24|32|40|
18|27|36|45|
20|30|40|50|

Or in right alignment:
Code:
#include<iostream>

using namespace std;

int main()
{
	int multiplier[]={2,3,4,5};
	int i, j;
	
	cout.setf(ios::right);
	
	for(i=1;i<11;i++)
	{
		for(j=0;j<4;j++)
		{
			cout.width(2);
			cout<<multiplier[j]*i;
			cout<<"|";
		}
		
		cout<<endl;
	}
	
	return 0;
}

Outputs as:
Code:
 2| 3| 4| 5|
 4| 6| 8|10|
 6| 9|12|15|
 8|12|16|20|
10|15|20|25|
12|18|24|30|
14|21|28|35|
16|24|32|40|
18|27|36|45|
20|30|40|50|

Right alignment with a last-column space as seen in your format (Looks unique and out of place).

Code:
#include<iostream>

using namespace std;

int main()
{
	int multiplier[]={2,3,4,5};
	int i, j;
	
	cout.setf(ios::right);
	
	for(i=1;i<11;i++)
	{
		for(j=0;j<4;j++)
		{
			cout.width(2);
			cout<<multiplier[j]*i;
			if(j==2)
			cout<<"| ";
			else
			cout<<"|";
		}
		
		cout<<endl;
	}
	
	return 0;
}

Outputs as:
Code:
 2| 3| 4|  5|
 4| 6| 8| 10|
 6| 9|12| 15|
 8|12|16| 20|
10|15|20| 25|
12|18|24| 30|
14|21|28| 35|
16|24|32| 40|
18|27|36| 45|
20|30|40| 50|

Or much more neater:

P.s. Without any variables except the counters.

Code:
#include<iostream>

using namespace std;

int main()
{
	cout.setf(ios::right);
	
	for(int i=1;i<11;i++)
	{
		for(int j=2;j<6;j++)
		{
			cout.width(2);
			cout<<j*i<<" | ";
		}
		
		cout<<endl;
	}
	
	return 0;
}

Outputs as:
Code:
 2 |  3 |  4 |  5 | 
 4 |  6 |  8 | 10 | 
 6 |  9 | 12 | 15 | 
 8 | 12 | 16 | 20 | 
10 | 15 | 20 | 25 | 
12 | 18 | 24 | 30 | 
14 | 21 | 28 | 35 | 
16 | 24 | 32 | 40 | 
18 | 27 | 36 | 45 | 
20 | 30 | 40 | 50 |
 
Last edited:

sakumar79

Technomancer
Yeah, its pretty easy... Get start number, end number and final multiplier number... Then, do a loop (i) for multiplier within which you do the inner loop (j) from the start number to the end number... In the inner loop, print i*j and '|', after the inner loop, give a carriage return within the outer loop...

Arun
 

QwertyManiac

Commander in Chief
Simple, you need to specify a width again.

Right justified:
Code:
#include<stdio.h>

int main()
{
    int i, j;
    for(i=1;i<11;i++)
    {
        for(j=2;j<6;j++)
        {
            printf("[B]%2d[/B] | ", (j*i));
        }
        
        printf("\n");
    }
}
Note that % [WIDTH] TYPE format.

Outputs as:
Code:
 2 |  3 |  4 |  5 | 
 4 |  6 |  8 | 10 | 
 6 |  9 | 12 | 15 | 
 8 | 12 | 16 | 20 | 
10 | 15 | 20 | 25 | 
12 | 18 | 24 | 30 | 
14 | 21 | 28 | 35 | 
16 | 24 | 32 | 40 | 
18 | 27 | 36 | 45 | 
20 | 30 | 40 | 50 |
Left Justified (Using a Flag, of course)
Code:
#include<stdio.h>

int main()
{
    int i, j;
    for(i=1;i<11;i++)
    {
        for(j=2;j<6;j++)
        {
            printf("%[B]-[/B]2d | ", (j*i));
        }
        
        printf("\n");
    }
}

Again, note the Note that % [Justification - FLAG] [WIDTH] TYPE format.

Outputs as:
Code:
2  | 3  | 4  | 5  | 
4  | 6  | 8  | 10 | 
6  | 9  | 12 | 15 | 
8  | 12 | 16 | 20 | 
10 | 15 | 20 | 25 | 
12 | 18 | 24 | 30 | 
14 | 21 | 28 | 35 | 
16 | 24 | 32 | 40 | 
18 | 27 | 36 | 45 | 
20 | 30 | 40 | 50 |
 
Status
Not open for further replies.
Top Bottom