aspose file tools*
The moose likes Beginning Java and the fly likes How to print this pattern using nested for loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to print this pattern using nested for loops" Watch "How to print this pattern using nested for loops" New topic
Author

How to print this pattern using nested for loops

MohamedSalim malik
Greenhorn

Joined: Feb 04, 2011
Posts: 19
***************
************
**********
*******
*****
***
*
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Can you post what you have tried so far?

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

Sean


I love this place!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

And welcome to the Ranch!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
MohamedSalim malik
Greenhorn

Joined: Feb 04, 2011
Posts: 19
---------------***************
-----------------*************
------------------***********
---------------------********
-----------------------******
------------------------*****
--------------------------***
---------------------------**
----------------------------*
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

Joined: Feb 04, 2011
Posts: 19
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

Joined: Oct 02, 2003
Posts: 11328
    
  16

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:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
MohamedSalim malik
Greenhorn

Joined: Feb 04, 2011
Posts: 19
I wanted the Exact pattern you have just mentioned.. the Upside down pyramid..

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11328
    
  16

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

Joined: Feb 04, 2011
Posts: 19
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.. :
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to print this pattern using nested for loops