469,903 Members | 2,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Need help on Looping

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
1 1101
Velhari
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.

Similar topics

5 posts views Thread by masood.iqbal | last post: by
21 posts views Thread by asif929 | last post: by
2 posts views Thread by Davaa | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.