The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Use for loops

Posted by Hiran on January 25, 2002 at 2:48 PM

Imran, the best way would probably be to use three for loops. One counting upwards and one counting downwards. You would also need a loop outputing the same amount of stars as the input number. Here's some code to help:

//assuming the input is 5
for (int i=1; i<5; i=i+2)
{
for (int j=1; j<(i+1); j++)
{
System.out.print("*");
}
System.out.print('\n');
}
for (int i=1; i<6; i++)
{
System.out.print("*");
}
System.out.print('\n');
for (int i=3; i>0; i=i-2)
{
for (int j=1; j<4; j++)
{
System.out.print("*");
}
System.out.print('\n');
}

Of course, you would need to substitute using variables (instead of the actual values). Also, if '\n' doesn't work, try "\\n", or create a character variable that holds \n (ie, char c = '\n') and output that variable. (By the way, if you don't know, the \n is the symbol for a new line). Hope this helps.
Hiran

> hello
> how r u all. i m fine by bless of Allah n hope u same.

> i just want to make this,
> if input=5 and if input=7
> then print like that then print like that
> * *
> *** ***
> ***** *****
> *** *******
> * *****
> ***
> *
> if some have any answer then reply immediately

> thanks






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us