-9

I have searched in many sites, but I couldn't find the exact output of the following pascal triangle. Can anyone help me out of how to get the following output.

            1
          1   1
        1   2    1
     1    3    3    1
   1    4    6    4   1
1    5    10   10    5   1
user1118321
  • 25,567
  • 4
  • 55
  • 86
Kanth
  • 47
  • 3
  • 6
  • 2
    So is your problem finding the numbers or formatting your output? Also, that's not pascal's triangle. – Thomas Jun 06 '12 at 16:45
  • Both.Because once it reaches maximum level in the row, it has to go to minimum value in that row again. If you can, please help me out. Because this was asked in interview exam. – Kanth Jun 06 '12 at 16:47
  • 1
    `Pascal` as you used it to tag refers to the programming language, and isn't appropriate for this question. Please choose your tags more carefully in the future; there's a description as you start to add them that tells you what they mean. Thanks. (Also, what have you tried so far that isn't working? We expect you to at least **try** to solve it yourself before asking here. Try, and then post your code that doesn't work and ask questions about it, and we'll try to help.) – Ken White Jun 06 '12 at 16:48
  • 1
    Thanks for not showing one ounce of effort. Voting to close. – Hovercraft Full Of Eels Jun 06 '12 at 16:51
  • It is really ludicrous. How could you think that I didn't try. I tried but I am getting ascending order output in each row rather than this format. I thought it was no use of pasting that program, when it couldn't give the output. – Kanth Jun 06 '12 at 17:44

4 Answers4

11

You can try

System.out.println(
        "        1\n" +
        "      1 2 1\n" +
        "    1 2 3 2 1\n" +
        "  1 2 3 4 3 2 1\n" +
        "1 2 3 4 5 4 3 2 1\n");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 9
    There are 10,000 comedians out of work right now... – Tony Ennis Jun 06 '12 at 16:49
  • Oh my god! what's this Peter? Anyone can print like that? If anyone needs output like that, everyone need not write logic, just printing that formats is enough. – Kanth Jun 06 '12 at 17:11
  • 2
    Perhaps you had a specific type of solution in mind. You can edit the question if you feel an answer does match what you intended to ask. ;) – Peter Lawrey Jun 06 '12 at 17:14
  • 1
    No good. You should make a class with public static final String values for each triangle size. It would be very easy to extend if the user wants to make bigger triangles :D – toniedzwiedz Jun 06 '12 at 17:16
  • @Peter: My intention is not to point out others' mistakes, I thought you were silly about me by giving such simple answer.Okay, anyways here you go- to write logic for that program in order to get that type of output,not just for printing that. – Kanth Jun 06 '12 at 17:19
  • 1
    You need to say what logic you have in mind. I can imagine a dozen ways to do this but I don't see any reason other that to give the simplest answer. i.e. what do you mean by "write logic" ? loops, recursions, OOP, AOP ... It sounds to me like; give me the second most obvious answer to this question, which is pretty vague. – Peter Lawrey Jun 06 '12 at 17:28
  • By loops. I am getting only ascending order numbers in each row rather than this format. Of course I believe every one can do it, but I need in the format that I mentioned in the question. – Kanth Jun 06 '12 at 17:39
  • 3
    Ok, how about `for (int i = 1; i < max * 2; i++) System.out.print(Math.min(i, max * 2 - i) + " ");` – Peter Lawrey Jun 06 '12 at 17:41
  • Okay. Thank you so much Peter. I tried in this way and I got output. Thanks for your logic. – Kanth Jun 06 '12 at 18:19
  • program class PTri { public static void main(String[] args) { int numrow=4; for(int max=1; max<=numrow; max++) { for(int j=1;j<=numrow-max;j++) { System.out.print(" "); } for (int i = 1; i < max * 2; i++) { System.out.print(Math.min(i, max * 2 - i) + " "); } System.out.println(); } } } – Kanth Jun 06 '12 at 18:26
3

Just gonna give you a little help with the formatting here. You should be able to handle the numbers part.

for(int i = 0; i < numberOfRows; i++)
{
    System.out.print(2 * (numberOfRows - i) * " "); //2 is gonna be the number of spaces between each entry. You could change it to whatever you want to stretch/shrink the triangle
    System.out.print(""); //pascal-specific stuff goes here
}

and by the way -- That isn't pascal's triangle. Pascal's triangle isn't linear like that. Pascal's triangle looks like:

            1
          1   1
        1   2    1
     1    3    3    1
   1    4    6    4   1
1    5    10   10    5   1
Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67
1

Think about the relationship between the number of spaces before your first number and the "level" you're on. Once you figure that, it should be easy to code.

If you can't code it, show us what you've tried so far.

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
0

Other answers have given hints as to the formatting, but you seem unclear on what pascal's triangle is. Once you understand what the triangle actually is conceptually, converting it to code should be relatively simple.

Thomas
  • 5,074
  • 1
  • 16
  • 12