aspose file tools*
The moose likes Java in General and the fly likes one logical question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "one logical question " Watch "one logical question " New topic
Author

one logical question

Mohammad Sufiyan Al Yousufi
Greenhorn

Joined: Aug 30, 2010
Posts: 11
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.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4387
    
    8

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

Joined: Aug 30, 2010
Posts: 11
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

Joined: Apr 06, 2010
Posts: 4387
    
    8

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.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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


SCJP 6. Learning more now.
Mohammad Sufiyan Al Yousufi
Greenhorn

Joined: Aug 30, 2010
Posts: 11
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

Joined: Jan 21, 2011
Posts: 637

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"
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one logical question