Help with C++ for Loops & Patterns

  • Context: C/C++ 
  • Thread starter Thread starter ranger
  • Start date Start date
  • Tags Tags
    C++ For loops Loops
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
ranger
Gold Member
Messages
1,687
Reaction score
2
I need some help. So far I have some for loops that print the following patters:

*
**
***
****
*****
******
*******
********
*********
**********

and **********
*********
********
*******
******
*****
****
***
**
*
Here is the code:
Code:
for(int rows=1;rows<=10;rows++)
		  {
			for(int asteriskcount=0;asteriskcount<rows;asteriskcount++)
					 {
					  cout<<"*";
					 }
					 cout<<endl;
		  }

		  cout<<endl<<endl;

for(int rows_2=1;rows_2<=10;rows_2++)
		  {
					 for(int asteriskcount_2=11;asteriskcount_2>rows_2;asteriskcount_2--)
					 {
								cout<<"*";
					 }
					 cout<<endl;
		  }

			 cout<<endl<<endl;

The first pair of for loops prints the first pattern and the second prints the second pattern. I need some help with a third pattern though. It should look smoething like this:

**********
*********
********
*******
******
*****
****
***
**
*
EDIT: It seems the third pattern is not posting the way I typed it. So I have uploaded a MS Word file containg it.

--thank you.
 

Attachments

Last edited:
Physics news on Phys.org
For the third pattern, you could use an 'if' statement in your innermost loop to print the the required number of spaces (ie, " ") for each row and then, print * for the rest of the row.
 
Code:
for (int i=0; i<=9; i++)
{
   for (int j=0; j<i; j++)
       cout << " ";
   for (int j=i; j<=9; j++)
        cout << "*"
   cout << endl;
}