Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (myname):

how to make triangular patterns by using nested loops in Java?

OpenStudy (anonymous):

Do you want something like this? * **** *******

OpenStudy (anonymous):

int Column = 5; int index = 0; int NumStars = 0; int NumSpaces = Column/2; while( NumSpaces >= 0 ) { NumStars = 2*index + 1; for( int i = 0; i < NumSpaces; i ++) System.print(" "); for( int i = 0; i < NumStars; i ++) System.print("*"); System.println(); index = index + 1; NumSpaces = NumSpaces - 1; }

OpenStudy (myname):

Thank You .I appreciate your help. But I wanted more of a number triangle I tried many times but couldn't figure it out. The output are as follows: run: Pattern I 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern II 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Pattern III 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Pattern IV 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 6 BUILD SUCCESSFUL (total time: 0 seconds

OpenStudy (anonymous):

To get a perfect triangle the width(which i call the column must be an odd number). Give me some time and i will come up with what you really want

OpenStudy (anonymous):

//MaxNumber is the largest possible number to be printed //Small to Big void Pattern1( int MaxNumber) { for( int i = 1; i <= MaxNumber; i++ ) { for( int j = 1; j <= i; j++ ) { System.print(j); } System.println(); } } //MaxNumber is the largest possible number to be printed //Big to Small void Pattern2( int MaxNumber) { for( int i = MaxNumber; i > 0 ; i-- ) { for( int j = 1; j <= i; j++ ) { System.print(j); } System.println(); } } char charToNum(char num) { switch(num) { case 0 : return '0'; case 1 : return '1'; case 2 : return '2'; case 3 : return '3'; case 4 : return '4'; case 5 : return '5'; case 6 : return '6'; case 7 : return '7'; case 8 : return '8'; case 9 : return '9'; default : return '\0'; } } String getNum(int num) { if( num < 1 ) return ""; if( num == 1 ) return "1"; return charToNum(num) + getNum(num-1) + charToNum(num); } //MaxNumber is the largest possible number to be printed //Small to Big void Pattern4( int MaxNumber) { int spaces = 0; for( int i = 1; i <= MaxNumber; i++ ) { spaces = MaxNumber - i; for( int j = 0; j < spaces; j++ ) { System.out.print(" "); } System.out.println( getNum( i ) ); spaces = spaces + 1; } }

OpenStudy (anonymous):

For the paterrn 3, the only way to get a really good triangle is to restrict the number s to odd.

OpenStudy (myname):

Great, it was such a good feeling to get even one of those pattern right. As i was trying this throughout the day.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!