• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to print this pattern using nested for loops

 
MohamedSalim malik
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
***************
************
**********
*******
*****
***
*
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post what you have tried so far?

From my thinking you would be splitting this question into 2 parts.

Sean
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
MohamedSalim malik
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------***************
-----------------*************
------------------***********
---------------------********
-----------------------******
------------------------*****
--------------------------***
---------------------------**
----------------------------*
actualy I wanted to print the above pattern.. it is a upside down triangle... it has 3 nested for loops.. can you please give a clue..
 
MohamedSalim malik
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Ex3{
public static void main(String args[]){
for(int i=9; i>=0 ;i--)
{
for(int j=0;i>=j ;j++)
{
System.out.print("*");
}
System.out.println(" ");

}
for(int i=1;i<=9;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print("*");
}
System.out.println(" ");

}

}

}


This is what i tried but it printed another pattern.. my java teacher says the upside down triangle can be printed using only 3 nested loops.. so please help me.. thank you..






 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
question...our forum software tries to re-format anything you post into neat, 'normalized' paragraphs. So do you want your pattern left-justified (like it displays here), or do you actually want a pyramid?

Note that if you use code tags, the software will preserve the spacing exactly as you enter it - even if it's not code. So you could do this:

 
MohamedSalim malik
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted the Exact pattern you have just mentioned.. the Upside down pyramid..

 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote it in 18 lines of code, with a for loop that contains two non-nested for-loops. Here is my output:

I approached it very systematically. First, I just tried to print the number of stars I wanted on each line. So, i wrote code that printed:
9
7
5
3
1

Once I got that, i worked to replace the line of code that printed a number with code that would print that many stars:
*********
*******
*****
***
*

Once I got that to work, I inserted code that printed spaces before I printed the stars.

This is what is called iterative development. You don't try and do it all at once...you write a VERY little bit of code, compile/debug/test it, and only once that part is working, you try doing the next step.
 
MohamedSalim malik
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Fred rosenberger .. Your method helped me alot.. : I followed those step by step method you have mentioned and i got the out put.. Thanks for sharing the idea.. :
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic