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:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Displaying output for the given format

Ranch Hand
Posts: 57
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
That program is not written by me :P

Rancher
Posts: 1776
• Number of slices to send:
Optional 'thank-you' note:
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: 13082
67
• Number of slices to send:
Optional 'thank-you' note:
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.

 I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising