| Author |
Displaying output for the given format
|
Subhash Pavuskar
Ranch Hand
Joined: Jun 29, 2011
Posts: 57
|
|
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
Joined: Apr 20, 2011
Posts: 73
|
|
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
Joined: Jun 29, 2011
Posts: 57
|
|
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
Joined: Apr 20, 2011
Posts: 73
|
|
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
Joined: Jun 29, 2011
Posts: 57
|
|
|
That program is not written by me :P
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
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
Joined: Oct 02, 2003
Posts: 10040
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: Displaying output for the given format
|
|
|