Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying output for the given format

 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Program to Display as below(input is 4) - turned out not to be so simple!
1
1 2 2 1
1 3 3 3 1
1 4 4 4 4 1
1 5 5 5 5 5 1

Can Anyone Explain me the logic ?
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to get it a bit. Lets try,

When i = 1, print 1 only once - 1
when i = 2, print 2, twice, but enclosed between 1 at the starting and ending - 1221
when i = 3, print 3, thrice, but enclosed between 1 at the starting and ending - 13331
when i = 4, print 4, 4 times, but enclosed between 1 at the starting and ending - 144441
when i = 5, print 5, 5 times, but enclosed between 1 at the starting and ending - 1555551

What I'm not getting is the relation of input value 4 with the output. I mean if the loop starts at 1 and is <=4 only this should get printed
1
1 2 2 1
1 3 3 3 1
1 4 4 4 4 1

Are you sure the input is 4? and not 5?
 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume that Input is :5

and show me the programming logic........

Actually i have program but i am not getting still logic behind it .



if you understood then comment on each single line.
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, use code tags while posting code. The code you posted is very hard to read as its not even formatted.

Secondly, I cant get how are you not getting the logic? Your wrote this code (hopefully), and it runs absolutely fine

Finally my logic for this, a bit simpler

 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That program is not written by me :P
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to understand a program that's already written... When I ran your program, it was having some additional spaces in the output like below

Enter input number
5
      1
    1 2 2 1
   1 3 3 3 1
 1 4 4 4 4 1
1 5 5 5 5 5 1

So you have to add starting and ending white spaces in Pranav's solution.

The code you posted with some clarification comments below... And use opening and closing braces for loops and conditions. It would be helpful for you to understand the program easily.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhash Pavuskar,

I added code tags to your post to make it more legible. next time highlight your code then hit the 'code' button above, just like you wanted to make it bold or italic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic