Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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: cout

Explanation / 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!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote