Loop in Turbo C++ .

closed account (EwkNwA7f)
Hi. Im having a hard time to formulate a code and the result of the output are

1. n=3
Output :
* * *
* *
*


2. n=3
Output :
3
3 2
3 2 1


Like the given example of our professor.

n=3
Output : 1
1 2
1 2 3

for(i=1;i<=n;i++)
{
for(j=1;j<=i;i++)
{
cout<<"j";
}
cout<<"\n";
}

A simple and not complicated code .
Thanks.
Last edited on
You will need the loops in reverse, ie:

1
2
3
4
5
6
7
8
for (int i=n; i>=1; i--)
{
    for (int j=i; j>=1; j--)
    {
         cout << j; // or * for your first output
    }
    cout << "\n";
}
Last edited on
closed account (EwkNwA7f)
a lot of asterisks is the result .
Sorry, the code I gave is only for the first, I thought you were after:


3 2 1
2 1
1

for the second. For

3
3 2
3 2 1

it is the exact same code as the example from your professor, except you are printing i+1-j, or alternatively change the second for loop in my example to
for(int j=n; j>=i; j--)
Last edited on
@queen321: Please, show your current version of code and the output, both in proper tags.
Last edited on
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
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

using namespace std;

void printAsc(int n)
{
	for (int i = 0; i <= n; ++i)
	{
		for (int j = 0; j <= i; ++j)
		{
			cout << j << " ";
		}
		cout << endl;
	}

}



void printDsc(int n)
{
	for (int i = n; i >= 0; --i)
	{
		for (int j = n; j >= i; --j)
		{
			cout << j << " ";
		}
		cout << endl;
	}
}


int main(void)
{
	printAsc(5);
	printDsc(5);
	return 0;

}
Topic archived. No new replies allowed.