By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Need help on Looping

P: 6
I am trying to write a program for an assignment that uses a For loop to produce an output that looks like this:

* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********

I started writing a code but i'm not sure how for example on the first triangle to increment 1 "*" on each new line. Can someone help me on incrementing 1 "*" on each of the next 10 lines to get the triangle pattern? Than I should be able to get the next 3 triangles. Thanks for your help.
[code]
public class Triangle
{
public static void main( String args [] )
{
for ( int j = 1; j <= 10; j ++ )
{
for ( int i = 1; i <= 10; i ++)
System.out.print ( '*');
System.out.println();
}
}
}

Output:
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
Press any key to continue . . .
Nov 14 '06 #1
Share this Question
Share on Google+
1 Reply


P: 46
I am trying to write a program for an assignment that uses a For loop to produce an output that looks like this:

* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********

I started writing a code but i'm not sure how for example on the first triangle to increment 1 "*" on each new line. Can someone help me on incrementing 1 "*" on each of the next 10 lines to get the triangle pattern? Than I should be able to get the next 3 triangles. Thanks for your help.
[code]
public class Triangle
{
public static void main( String args [] )
{
for ( int j = 1; j <= 10; j ++ )
{
for ( int i = 1; i <= 10; i ++)
System.out.print ( '*');
System.out.println();
}
}
}

Output:
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
Press any key to continue . . .
Hi,
I think the following code will hopefull to you................
[code]
public class Triangle
{
public static void main(String args[])
{
int k=1;
int m=10;
while ( k!=10 && m!=1 )
{
for ( int i=1; i<=k; i++ )
{
System.out.print ("*");
}
System.out.print(" ");
for ( int i=1; i<=m; i++ )
{
System.out.print ("*");
}
System.out.print (" ");
for ( int i=1; i<=m; i++ )
{
System.out.print ("*");
}
System.out.print (" ");
for ( int i=1; i<=k; i++ )
{
System.out.print ("*");
}
System.out.println();
k++;
m--;
}
}
}
Nov 14 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.