Name your source code as .txt (replace the text with with your last name and fir
ID: 3783124 • Letter: N
Question
Name your source code as .txt (replace the text with with your last name and first name, in that order), print out with your program and the output visible. Write a simple program in C++ to display the output below exactly as shown (i.e., it should include white space to position the pyramid toward the right, as shown). You can start your program as far to the left as possible, it doesn't need to be as far over as shown below. Your output should like up as is shown below. It should end up looking like a triangle. Alternate the characters you use as shown below You are not allowed to use a "string" data type in your code. So DO NOT take an approach similar to the following: coutExplanation / Answer
#include <iostream>
using namespace std;
int main()
{
int rows,k=0;
cout<<"Enter the number of rows: ";
cin>>rows; // get number of rows from the user to print the lines
for(int i=1;i<=rows;++i)
{
for(int space=1;space<=rows-i;++space)
{
cout<<" ";// printing the spaces
}
while(k!=2*i-1) // if k not equal to 2*i -1
{
if(i%2==1) // condition when the line number is odd then print the *
cout<<"* ";
else // condition when the line number is even then print the ^
cout<<"^ ";
++k; // incrementing k
}
k=0;
cout<<" "; //printing a new line after each line
}
return 0;
}
------------------output-----------------
Enter the number of rows: 6
*
^ ^ ^
* * * * *
^ ^ ^ ^ ^ ^ ^
* * * * * * * * *
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
----------output-----------------
// Note: Please feel free to ask any question/doubts. God bless you!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.