How do I get this pattern?

Thread Solved

Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

How do I get this pattern?

 
0
  #1
Jul 21st, 2009
Here's the pattern:

  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):
  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?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: How do I get this pattern?

 
0
  #2
Jul 21st, 2009
I am using another code to get only the top up-side down triangle:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: gautam610 has a little shameless behaviour in the past 
Solved Threads: 3
gautam610 gautam610 is offline Offline
Newbie Poster

Re: How do I get this pattern?

 
0
  #3
Jul 21st, 2009
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 .........................................................
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 76
Reputation: Lensva has a little shameless behaviour in the past 
Solved Threads: 3
Lensva Lensva is offline Offline
Junior Poster in Training

Re: How do I get this pattern?

 
-1
  #4
Jul 21st, 2009
  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC