LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2009, 06:40 AM   #1
savitrasapre
LQ Newbie
 
Registered: Mar 2008
Posts: 24

Rep: Reputation: 0
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
 
Old 05-17-2009, 06:45 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 05-17-2009, 08:23 AM   #3
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
As well, please post in between [ code] [/code ] tags to make your code more readable.
 
Old 05-17-2009, 12:10 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Doesn't take a rocket surgeon to read a few for loops... ;-)
 
Old 05-17-2009, 12:37 PM   #5
savitrasapre
LQ Newbie
 
Registered: Mar 2008
Posts: 24

Original Poster
Rep: Reputation: 0
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
 
Old 05-18-2009, 04:20 AM   #6
savitrasapre
LQ Newbie
 
Registered: Mar 2008
Posts: 24

Original Poster
Rep: Reputation: 0
ps what is a rocket surgeon anyways :P
either its a rocket scientist or a surgeon....??
 
Old 05-18-2009, 04:25 AM   #7
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
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.
 
Old 05-19-2009, 01:34 AM   #8
savitrasapre
LQ Newbie
 
Registered: Mar 2008
Posts: 24

Original Poster
Rep: Reputation: 0
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();
}

}

}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
triangle notification for new downloads gibney Linux - Newbie 4 06-01-2008 04:51 PM
Cannot print using Java 1.5 arubin Slackware 7 07-19-2007 10:04 AM
Java applications does not print on fedora core 5 erroldennis Linux - Software 0 07-02-2006 08:33 PM
print problem with Java and C JNI trutnev Programming 0 06-20-2005 11:28 AM
java print service rno Linux - Hardware 0 10-06-2003 05:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration