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 |
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.
|
As well, please post in between [ code] [/code ] tags to make your code more readable.
|
Doesn't take a rocket surgeon to read a few for loops... ;-)
|
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
|
ps what is a rocket surgeon anyways :P
either its a rocket scientist or a surgeon....?? |
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. |
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. |