Declare variables for outer counter, inner counter, size offigure Initialize cou
ID: 3615317 • Letter: D
Question
Declare variables for outer counter, inner counter, size offigure
Initialize counters to 0
Start outer loop to count up the lines
Start inner loop to count up the number of *
Start outer loop to count down the lines
Start inner loop to count down the number of *
Final pattern
*
**
***
****
*****
****
***
**
*
#include <iostream>
using namespace std;
const int HEIGHT = 5;
int main()
{
int i = 0, j = 0;
cout << endl;
while (i < HEIGHT)
{
i++;
j =0;
while (j< i)
{
j++;
cout << "*" ;
}
cout<< endl;
}
while (i > 0)
{
i--;
j =0;
while (j< i)
{
j++;
cout << "*" ;
}
cout<< endl;
}
system("PAUSE");
return 0;
}
Explanation / Answer
//Here is the programwithout counter while loop. //Hope this will helpyou.#include <iostream> using namespace std; const int HEIGHT = 5; int main() { int i = 0, j = 0,h; cout << endl; while (i < HEIGHT*2) { if(i<HEIGHT) h=i%HEIGHT; else { h= HEIGHT - i%HEIGHT; } i++; j = 0; while (j < h) { j++; cout<< "*" ; } cout <<endl; } system("PAUSE"); return 0; }
#include <iostream> using namespace std; const int HEIGHT = 5; int main() { int i = 0, j = 0,h; cout << endl; while (i < HEIGHT*2) { if(i<HEIGHT) h=i%HEIGHT; else { h= HEIGHT - i%HEIGHT; } i++; j = 0; while (j < h) { j++; cout<< "*" ; } cout <<endl; } system("PAUSE"); return 0; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.