nested for loops

# include<iostream>
# include <cstring>
using namespace std;
int main ()
{

for (int i=1; i<=18; i++)
{
for (int k=1; k<=i; k++)
{
cout << "*";
}
cout << endl;
for (int j=18; j>=i; j--)
{
cout << endl;
}

}
cout << endl;

system("pause");
return 0;
}
/*
****************** - 18
***************
*************
**********
*******
****
*/
Assertion "forum.post[0].hasQuestion()" failed.
Aborted

Last edited on
are sure this is nested loop application.
i think it is better for combination loop and if..

# include<iostream>
# include <cstring>
using namespace std;
int main ()
{

int j=3;
for (int i=0; i<=18; i++)
{
if (i==17)
{
cout << endl;
i=j;
j+=3;
}
cout << "*";
}
cin >> j;
return j;
}
What is the point of this exercise?
Just to start off printing 18 stars and then 3 stars less on successive lines??

if so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int someValue = 18;
	
	for (int x = someValue; x >0; x-=3)
	{
		cout << string(x, '*') << endl;	   	   
	}

        return 0;

}
How do u move the all lot of stars to the middle well balance to make a pyramid
*******************
**************
**********
******

What is the point of this exercise?
Just to start off printing 18 stars and then 3 stars less on successive lines??


Actually the point of the exercise is familiarization with nested for loops, we did the same thing in school a few years ago. Then you can later relate this to using nested loops to traverse 2D arrays. Though we actually made diamond and hourglass shapes instead of an upside-down pyramid.

How do u move the all lot of stars to the middle well balance to make a pyramid

add spaces, using a loop as well. Though you can actually do that exercise using only 2 loops, I'll show you, only after you solve the exercise though.
Last edited on
Topic archived. No new replies allowed.