• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to print this pattern using nested for loops

 
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
***************
************
**********
*******
*****
***
*
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
MohamedSalim malik
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted the Exact pattern you have just mentioned.. the Upside down pyramid..

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.. :
reply
    Bookmark Topic Watch Topic
  • New Topic