This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

one logical question  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

I have a requirement. I need a program which shows the output like below



As we all know, we can show this using multidimention arrays. Here codition is, you can't use the arrays. Without using arrays, I have to get the above output. I tried but not getting.

till now I tried this



This is giving the output

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5


Please help me to get the required output.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So within the outer loop (before the inner loop), you need to write some spaces. That's straightforward enough, if you just know how many spaces.

i = 1: 0 spaces
i = 2: 3 spaces
i = 3: 6 spaces
i = 4: 9 spaces
i = 5: 12 spaces

So you need to write 3*(i - 1) spaces. Is that enough of a clue?
 
Mohammad Sufiyan Al Yousufi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying. Yes, it is known how many spaces needed. But how can we put spaces. If we change n value then it should display based on the new value.

I mean, how we controle the execution to print spaces... Sorry if I sounds dump.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can print out the numbers 1-n (which you're already doing), surely you can print n spaces? It just needs another loop.

There are also "neater" ways - System.out.format() gives you some interesting options if you want to investigate that.

By the way, I think the code would be a bit easier to follow if you used a for loop for the inner loop, as well as the outer. for is generally better than while if you're just counting through a set that is known at the start of the loop.
 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understand what is being asked of you...Tell yourself that you can do it...(i failed 3 times while making this code)

If you develop a mathematical relation between the line number to be printed, the number of spaces , and digits...the task will become simple.
LAST number is always 5.

line 0 , spaces 0*3 , digits 0+1 to 5
line 1 , spaces 1*3 , digits 1+1 to 5
line 2 , spaces 2*3 , digits 2+1 to 5
etc...

Only after you have grasped the above , analyze this code, and then run it :



hth

regards
rb
 
Mohammad Sufiyan Al Yousufi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Matthew, really nice explaination. Your explaination helps me to understand the flow.

Rahul you are really great. I tried so much but not getting it. Really nice. How can I improve my logical skills to do these types of questions?
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohammad Sufiyan Al Yousufi wrote:Thank you Matthew, really nice explaination. Your explaination helps me to understand the flow.

Rahul you are really great. I tried so much but not getting it. Really nice. How can I improve my logical skills to do these types of questions?



I am not great. I am new to java. Dont worry, find more problems and solve them. You will become better with practice. And if its logic you like, solve "good" puzzles (like in news paper)...that will sharpen you for coding !

Good luck and keep coding...and as bryan adams would like it "keep coding till the fingers bleed"
 
lowercase baba
Posts: 12706
50
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one suggestion about the code written by Rahul Sudip Bose...You should ALWAYS use curly braces on all for loops, even when only one line. If you don't, I guarantee that some day, you will put in a print statement or some other line of code in what you think is the loop body, and your code will break.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!