• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

nested loop ?

 
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
*
**
***
****
*****
****
***
**
*
 
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.
 
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).]
 
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
 
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).]
 
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
 
I knew that guy would be trouble! Thanks tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic