LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to print a triangle in between in java (https://www.linuxquestions.org/questions/programming-9/how-to-print-a-triangle-in-between-in-java-726552/)

savitrasapre 05-17-2009 06:40 AM

how to print a triangle in between in java
 
public class Pattern {


public static void main(String[] args) {

int i,j;


for(i=4;i>0;i--)
{
for(j=i-1;j>0;j--)
{
System.out.print("-");
}
System.out.print("*");


/*
for(i=0;i<3;i++)
{
for(j=0;j<(2*i)+1;j++)
{
System.out.print("-");
}
System.out.println();
}

*/



System.out.print("*");

for(j=i-1;j>0;j--)
{
System.out.print("-");

}

System.out.println();
}

}

}

here i want the commented loop to run in between the astrix so that i need the output as-


---*---
--*-*--
-*---*-
*-----*


i know its lame but i cudnt connect it

acid_kewpie 05-17-2009 06:45 AM

Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.

Alien_Hominid 05-17-2009 08:23 AM

As well, please post in between [ code] [/code ] tags to make your code more readable.

acid_kewpie 05-17-2009 12:10 PM

Doesn't take a rocket surgeon to read a few for loops... ;-)

savitrasapre 05-17-2009 12:37 PM

guys actually this is not at all a homework assignment,i just want to print the triangle like i have already shown and plus i just wanted to practise my looping but i am stuck here for the past few days,that is y im seeking for help and then ill move on to other more important topics

savitrasapre 05-18-2009 04:20 AM

ps what is a rocket surgeon anyways :P
either its a rocket scientist or a surgeon....??

Alien_Hominid 05-18-2009 04:25 AM

Have you fixed your program? One error which is clearly seen is that your code is unable to print top line because it always prints two stars (*).

It's just acid kewpie teasing me.

savitrasapre 05-19-2009 01:34 AM

yes i have thanks a lot anyways

public class Pattern {

public static void main(String[] args) {

int i, j,n=4;

for(i=0;i<4;i++,n--)
{
for(j=0;j<n;j++)
{
System.out.print(" ");
}

System.out.print("*");

for(j=1;j<(2*i)+1;j++) { System.out.print(" ");
}

System.out.print("*");

for(j=0;j<n;j++)
{
System.out.print(" ");
}

System.out.println();
}

}

}


All times are GMT -5. The time now is 09:04 PM.