943,549 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 520
  • Java RSS
Jul 21st, 2009
0

How do I get this pattern?

Expand Post »
Here's the pattern:

Java Syntax (Toggle Plain Text)
  1. *******
  2. *****
  3. ***
  4. *
  5. **
  6. ***
  7. *****
  8. ********

Here's the code I used to get only the first part(the up-side down triangle):
Java Syntax (Toggle Plain Text)
  1. class hourglass
  2. {
  3. static void pattern()
  4. {
  5. for(int j=1;j<=4;j++)
  6. {
  7. for(int i=1;i<=j;i++)
  8. {
  9. System.out.print(" ");
  10. }
  11. for(int i=7;i>j;i--)
  12. {
  13. System.out.print("*");
  14. }
  15. System.out.println();
  16. }
  17. }
  18. }

It isn't working, can anybody help?
Similar Threads
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Jul 21st, 2009
0

Re: How do I get this pattern?

I am using another code to get only the top up-side down triangle:

Java Syntax (Toggle Plain Text)
  1. class hourglass
  2. {
  3. static void pattern()
  4. {
  5. for(int j=1;j<=4;j++)
  6. {
  7. for(int i=1;i<=j;i++)
  8. {
  9. System.out.print(" ");
  10. }
  11. for(int i=4;i>=1;i--)
  12. {
  13. for(int p=1;p<=i;p++)
  14. {
  15. System.out.print("*");
  16. }
  17. }
  18. System.out.println();
  19. }
  20. }
  21. }
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Jul 21st, 2009
0

Re: How do I get this pattern?

Hello,
you can get the pattern by using "if and else" in the loop. Since it is not a continuous pattern so doing this throw a loop is not simple task .........................................................
Reputation Points: 5
Solved Threads: 3
Newbie Poster
gautam610 is offline Offline
13 posts
since Jul 2008
Jul 21st, 2009
-1

Re: How do I get this pattern?

java Syntax (Toggle Plain Text)
  1. public class A {
  2. public static void main(String[] main) {
  3. int length=7;
  4.  
  5. for(int i=length; i>0; i-=2) {
  6. for(int j=length-i; j>=0; j--) System.out.print(" ");
  7. for(int k=i; k>0; k--) System.out.print("*");
  8. if(i!=1) System.out.println();
  9. }
  10. for(int i=-1; i<=length; i+=2) {
  11. for(int j=0; j<=length-i; j++) System.out.print(" ");
  12. for(int k=0; k<i; k++) System.out.print("*");
  13. System.out.println();
  14. }
  15.  
  16. }
  17. }
Last edited by Lensva; Jul 21st, 2009 at 11:26 am.
Reputation Points: 4
Solved Threads: 3
Junior Poster in Training
Lensva is offline Offline
76 posts
since Mar 2008
Nov 5th, 2010
-1
Re: How do I get this pattern?
this
this is
this is a
this is a test
how to print this??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahrukhali5 is offline Offline
1 posts
since Nov 2010
Nov 5th, 2010
0
Re: How do I get this pattern?
Use an array and a loop.

Next time start a new thread if you have a question. Don't hijack someone else's.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help
Next Thread in Java Forum Timeline: variable is printing the wrong values





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC