Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to print this pattern using nested for loops  RSS feed

 
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
***************
************
**********
*******
*****
***
*
 
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
 
Sheriff
Posts: 21421
94
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..






 
lowercase baba
Bartender
Posts: 12627
50
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: 12627
50
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.. :
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!