Write a program that uses for statements to print the following patterns separat
ID: 3634809 • Letter: W
Question
Write a program that uses for statements to print the following patterns separately, one below the other. Usefor loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout <<
'*'; (this causes the asterisks to print side by side). [Hint: The last two patterns require that each line begin with
an appropriate number of blanks.] Extra credit: combine your code from the four separate problems into s single program that prints all four patterns side by side by making use of a nested for loops.
a- b- c- d-
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********
Explanation / Answer
Please Rate:Thanks
Pattern 1:
#include<iostream.h>
void main()
{
for(int i=1;i<=10;i++)
{
for(int j=1;j<=i;j++)
cout<<"*";
cout<<" ";
}
}
-----------------------------------------------------------------------------------------------------------------------
Pattern 2:
#include<iostream.h>
void main()
{
for(int i=10;i>=1;i--)
{
for(int j=10;j>=i;j--)
cout<<" ";
for(int k=1;k<=i;k++)
cout<<"*";
cout<<" ";
} }
------------------------------------------------------------------------------------------------------------
Pattern 3:
#include<iostream.h>
void main()
{
for(int i=10;i>=1;i--)
{
for(int j=1;j<=i;j++)
cout<<"*";
cout<<" ";
} }
-------------------------------------------------
Pattern 4:
#include<iostream.h>
void main()
{
for(int i=10;i>=1;i--)
{
for(int j=1;j<=i;j++)
cout<<" ";
for(int k=10;k>=i;k--)
cout<<"*";
cout<<" ";
} }
------------------------------------------------------------------------------
Combine 4 patterns program
#include<iostream.h>
void main()
{
cout<<"a-b-c-d"<<endl;
for(int i=1;i<=10;i++)
{
for(int j=1;j<=i;j++)
cout<<"*";
cout<<" ";
for(int k=10;k>=i;k--)
cout<<"*";
cout<<" ";
for(int l=10;l>=i;l--)
cout<<"*";
cout<<" ";
for(int m=1;m<=i;m++)
cout<<"*";
cout<<" ";
cout<<" ";
}
}
-----------------------------------------------
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.