Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nested loop ?

 
ADNAN KHAN
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ASSALAM-O-ALAKUM:-
I HAVE A QUESTION ABOUT NESTED LOOP WHIHC I CAN NOT UNEDERSTAND.
Q):- Write a program that ask the user to enter the size of triangle to print out (an integer from 1 to 50, then print the triangle by printing a series of lines with asterisks. The first line wil have one asterisk,the next two,etc. each line having one more asterisk thaan the previous line up to the number entered by the user. on the next line print one liess asterisk and continue by decreasing the number of asterisks by one for each successive line until only one asterisk is printed. Hint: use nested loop. for example if user input 5 the output would be
*
**
***
****
*****
****
***
**
*
 
Andy Ceponis
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets see what you have so far and maybe someone can point ya in the right direction.
 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ADNAN
To get the required output
*
**
***
****
*****
****
***
**
*
In this example
look at the number of columns - 5, dont they represent the no. u entered. At each iteration, you print that character that many no. of times.
1st Iteration - print '*' 1 time(s)
2nd Iteration - print '*' 2 times
3rd Iteration - print '*' 3 times
so on...
so for printing '*' n times you must use another loop to achieve this.

Hope this helps
(edited by Cindy to add formatting)
[This message has been edited by Cindy Glass (edited March 26, 2001).]
 
usman ahmad
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one Big nested loop. Siva could you please elaborate a bit more so that I can also understand fully.Thanks a lot.
regards
Usman
 
Lawrence Thurman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this out - Yeah it is a little more involved but
it will work just copy and paste

[This message has been edited by Marilyn deQueiroz (edited March 27, 2001).]
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lawrence,
To create your int named "count", why not just do it like so:
<h4>int count = Integer.parseInt( args[0] ) ;</h4>
This is alot easier than converting the string to an Integer object and then to an int...it skips an unnecessary step.
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic