• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Displaying output for the given format

 
Ranch Hand
Posts: 57
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That program is not written by me :P
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic