C++ while loops

closed account (41U4izwU)
Hi, i'm new to coding and i recently posted a question, but it still hasn't been resolved. I was hoping that someone can help me with the question. I really need help on creating triangles in nested while loops, Ive already made some, but there is one that I am really stuck on.
*
**
***
****
*****
******
*******
********
*********
********** \\ I have made a code for this:

int counter=1;

while (counter<=10){

int counter2=1;

while (counter2<counter){

cout<<"*";

counter2=counter2+1;

}

cout<<"*"<<"\n";

counter=counter+1;

}
I am not sure of how to do this one:
**********
--*********
----********
------*******
--------******
----------*****
------------****
--------------***
----------------**
------------------*\\ The dashes are meant to be spaces.


We did this problem also I think. Here is my full code, with the asterisk triangle oriented four ways:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Program to make asterisk pyramids
#include<iostream>  /* header file for input and output */

using namespace std;
int main ()      /* allow operations from standard library */
{
    int r, c, d; //initialize variables
    
    for(r=0; r<10; r++) //loop to make the # of asterisks increase each row
    {  
             for (c=0; c<r+1; c++) //nested for loop for asterisk period facing right
             {
                 cout<<"*"; //print asterisk to form one side of square, pyramid
                 }
             for (c=r; c<10; c++) //nestecd for loop for other half of square, in spaces
             {
                 cout<<" "; //print spaces to form other half of pyramid
                 }  
                 cout<<'\t'; //tab
             for (c=r; c<10; c++) //loop for upside-down pyramid
             {
                 cout<<"*"; //print pyramid
                 }
              for (d=r+1; d>0; d--) //spaces for other half of pyramid
             {
                 cout<<" "; //print spaces
                 }
                 cout<<'\t'; //tab
                  for (d=r; d>0; d--) //spaces for reverse pyramid
             {
                 cout<<" "; //print spaces
                 }
               
                for (c=r; c<10; c++) //asterisk for reverse pyramid
             {
                 cout<<"*"; //print asterisks
                 }  
                 cout<<'\t'; //tab
                  for (d=r; d<10; d++) //spaces for upside-down reverse pyramid
             {
                 cout<<" "; //print spaces
                 }
                  for (c=r+1; c>0; c--) //asterisks for upside-down reverse pyramid
             {
                 cout<<"*"; //print asterisks
                 }
             cout<<endl; //end the line to form the pyramids
    }
    cout<<endl;
    
    
    system ("pause"); //leaves window up
    return 0; //End of main function
}


I suggest compiling this to figure out which triangles are which and going from there!
Last edited on
closed account (18hRX9L8)
Remember not to use system().
http://www.cplusplus.com/forum/articles/11153/
closed account (41U4izwU)
Thank you all for helping me with this, I really appreciate it!
If you are required to use nested while loops, use them.
And try to extend the program that you have written to solve the first problem.

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
#include <iostream>

int main()
{
    int counter = 1 ;

    while( counter <= 10 ) // for each line 1 to 10
    {
        int counter2 = 1 ;

        while( counter2 < counter ) // first 'counter' characters are spaces
        {
            std::cout << ' ' ;
            counter2 = counter2+1 ;
        }

        while( counter2 < 10 ) // remaining are asterisks
        {
            std::cout << '*' ;
            counter2 = counter2+1 ;
        }

        std::cout << '\n' ; // and the line ends with a new line

        counter = counter + 1 ;
    }
}
Topic archived. No new replies allowed.