table of numbers from 50 to 150

in this we have to show table of 50 to 150
like 50 table from 1 to 10
then 51 table ......150 table
i just want to know is there any way i can get this using loop not using nested loop
i made the nested loop code to show you what i want on ouput
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<endl;
for(int i=50;i<=150;i++)
{
	cout<<"\tTable of "<<i<<endl<<endl;
	for(int j=1;j<=10;j++)
	{

   cout<<"\t"<<i<<" * "<<j<<" = "<<(i*j)<<endl;

   }
   cout<<endl;
   getch();
   }
getch();
}
Last edited on
A couple of things.

You must be using an old or out of date compiler. To be standard-compliant, the include file you want is <iostream>, not <iostream.h>, and you will need to use std::cout and std::endl (or using namespace std;). Also, main() must return an int, not void.

Anyway, you do not have a nested loop. You have 2 loops running concurrently. A nested loop would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
int main()
{
	int num;
	cout<<endl;
	for(int i=50; i<=150; i++)
	{
		for (int j = 1; j <= 10; j++)
		{
			cout<<"\t"<<i<<" * "<<j<<" = "<<(i*j)<<endl;
		}
	}
}
i accidentally paste the wrong code sorry
now its correct
i just need to know that can i achieve the output without using nested loop
I don't think you can. If you want to get every combination of i and j, you need to nest the loops.
Could you show how the output shall look?

50 * 1 =50
50 * 2 =100
50 * 3 =150
50 * 4 =200
50 * 5 =250
50 * 6 =300
50 * 7 =350
50 * 8 =350
50 * 9 =450
50 * 10 =500
51 * 1 =51
51 * 2 =102
51 * 3 =153
51 * 4 =204
51 * 5 =255
51 * 6 =306
51 * 7 =357
51 * 8 =408
51 * 9 =459
51 * 10 =510

it will continue to 150 table
Last edited on
If you're dead set on not using nested loops, then you could probably kludge it with some fiddly maths and a bunch of if statements. But why would you bother? Using nested loops is much simpler and cleaner.
@tjnapster555

Here is one way to do the table and NOT use a nested loop, though I guess a while loop may constitute to be type of nested loop. Anyway, here it is..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Table.cpp : main project file.

#include<iostream>
#include<conio.h>

int main()
{
	int num,j=1;
	std::cout << std::endl;
	for(int i=50;i<=150;i++)
	{
		if (j=1)
			std::cout<<"\tTable of "<<i<<std::endl<<std::endl;
		while(j<11)
		{
		std::cout<<"\t"<<i<<" * "<<j<<" = "<<(i*j)<<std::endl;
		j++;
		}
		std::cout << "Press enter for next table results.." << std::endl;
		_getch();
		j=1;
	}
	std::cout<<std::endl;
	_getch();
return 0;
}
@itenite1
It is obvious that your code has a nested loop.
Am I missing something, or is everyone humping this doorknob on purpose?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int i = 50; i <= 150; i++)
    {
        int j = 1;
        std::cout << i << " * " << j << " = " << i * j << std::endl; j++; // 1
        std::cout << i << " * " << j << " = " << i * j << std::endl; j++; // 2
        std::cout << i << " * " << j << " = " << i * j << std::endl; j++; // 3
        std::cout << i << " * " << j << " = " << i * j << std::endl; j++; // 4
        std::cout << i << " * " << j << " = " << i * j << std::endl; j++; // 5
        std::cout << i << " * " << j << " = " << i * j << std::endl; j++; // 6
        std::cout << i << " * " << j << " = " << i * j << std::endl; j++; // 7
        std::cout << i << " * " << j << " = " << i * j << std::endl; j++; // 8
        std::cout << i << " * " << j << " = " << i * j << std::endl; j++; // 9
        std::cout << i << " * " << j << " = " << i * j << std::endl; // 10
    }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>

int main()
{
	for ( int n = 1, i = 50; i <= 150; i += n / 10, n = n % 10 + 1 )
	{
		std::cout << std::setw( 3 ) << i << " * " 
		          << std::setw( 2 ) << n << " = " 
			  << std::setw( 4 ) << i * n << std::endl;
	}
}
Last edited on
Topic archived. No new replies allowed.