{"id":160,"date":"2011-02-13T15:32:34","date_gmt":"2011-02-13T04:32:34","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=160"},"modified":"2015-11-23T13:15:55","modified_gmt":"2015-11-23T02:15:55","slug":"pascals-triangle","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/pascals-triangle\/","title":{"rendered":"Pascals Triangle"},"content":{"rendered":"

\"\"
\nWrite a Java application that prints the first 10 lines of Pascals Triangle.<\/p>\n

Each row of a Pascals Triangle can be calculated from the previous row so the core of the solution is a method that calculates a row based on the previous row which is passed as input. Once we have that it is simply a matter of calling that method in a loop and formatting each row of the triangle.<\/p>\n

<\/div>\n
\nYou will notice that some padding is added to center the triangle output. This uses the row number to determine the amount of padding to use. This is not a perfect solution because it assumes that the numbers in the triangle are only single digits (which is not the case after row 5) but its a sufficient approximation for this example.
\nx<\/span><\/div>\n
\r\npackage com.learnjava.math;\r\n\r\n\/**\r\n * Write a Java program that calculates the \r\n * first 10 lines of a Pascals Triangle\r\n * \r\n * @author https:\/\/learn-java-by-example.com\r\n *\r\n *\/\r\n\r\npublic class PascalTriangle {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tint n = 10;\r\n\t\t\r\n\t\tint[] row = new int[0];\r\n\t\t\r\n\t\tfor (int i=0; i<n; i++) {\r\n\t\t\t\r\n\t\t\t\/\/ Calculate next row\r\n\t\t\t\r\n\t\t\trow = pascalRow(row);\r\n\r\n\t\t\t\/\/ Output row\r\n\t\t\t\/\/ First add some padding so triangle is centered\r\n\t\t\t\r\n\t\t\tfor (int j=0; j< n - i; j++) {\r\n\t\t\t\tSystem.out.print(" ");\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/ Now output the row values\r\n\t\t\t\r\n\t\t\tfor (int j=0; j< row.length; j++) {\r\n\t\t\t\tSystem.out.print(row[j]+" ");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t\t\r\n\t\t}\r\n\t}\r\n\r\n        \/\/ Full source available for Download\r\n        \/\/ See below for details\r\n}\r\n\r\n<\/pre>\n
\nFull source code for this example including the pascalRow() method is available for download<\/a> for our members.
\nx<\/span><\/div>\n

If you found this problem a little challenging then we would suggest having a look at some simpler examples of printing triangles using Java<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

Write a Java application that prints the first 10 lines of Pascals Triangle. Each row of a Pascals Triangle can be calculated from the previous row so the core of the solution is a method that calculates a row based on the previous row which is passed as input. Once we have that it is…<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[104,6,36,101,37],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-2A","jetpack-related-posts":[{"id":222,"url":"https:\/\/learn-java-by-example.com\/java\/list-array\/","url_meta":{"origin":160,"position":0},"title":"Using a List instead of an array","date":"March 8, 2012","format":false,"excerpt":"Lists and arrays can both be used to store ordered collections of data. Both have their strengths and weakness which we shall discuss in a later post. Previously we showed you how to generate Pascals Triangle and in that Java example we used arrays to represent each row of the\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":355,"url":"https:\/\/learn-java-by-example.com\/java\/printing-triangles-java\/","url_meta":{"origin":160,"position":1},"title":"Printing Triangles in Java","date":"November 23, 2015","format":false,"excerpt":"A common problem many new Java developers is to write a program that prints out a triangle. There a lots of variations on this problem but lets start with a simple case and then have a look at some possible variations. Probably the simplest case is a left aligned triangle\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":199,"url":"https:\/\/learn-java-by-example.com\/java\/pythagoras-theorem\/","url_meta":{"origin":160,"position":2},"title":"Pythagoras Theorem","date":"December 13, 2010","format":false,"excerpt":"One of our member students was asked to implement the Pythagoras Theorem using Java. This is a good opportunity to introduce the Math class which contains a collection of static methods for various mathematical functions. We need the square root method (sort) to calculate the hypotenuse, and can also use\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":166,"url":"https:\/\/learn-java-by-example.com\/java\/month-calendar\/","url_meta":{"origin":160,"position":3},"title":"Month Calendar","date":"May 1, 2011","format":false,"excerpt":"The following application shows how to format displaying a month in a way that would be suitable for including in a calendar. This involves making sure days of the week are all vertically aligned, and that the first day of the week appears in the first column. The day that\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/learn-java-by-example.com\/wp-content\/uploads\/2011\/05\/calendar.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":214,"url":"https:\/\/learn-java-by-example.com\/java\/convert-inches-centimetres\/","url_meta":{"origin":160,"position":4},"title":"Convert Inches to Centimetres","date":"October 14, 2011","format":false,"excerpt":"The next example was kindly shared by one of our members. She had a Java Swing assignment to create a Swing GUI for converting Inches to Centimetres and had no idea where to start. We helped her break down the problem and come up with a solution. The resulting code\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":300,"url":"https:\/\/learn-java-by-example.com\/java\/control-decimal-places-displayed-jtable-column\/","url_meta":{"origin":160,"position":5},"title":"How to control decimal places displayed in JTable column?","date":"September 10, 2015","format":false,"excerpt":"Rendering of table cells is handled by instances of TableCellRenderer. By default JTable uses a DefaultTableCellRenderer to render all of its cells. To control the number of decimal places used we just need to subclass DefaultTableCellRenderer and format the cell double value before passing the (formatted) value to to the\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/160"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=160"}],"version-history":[{"count":7,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":367,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/160\/revisions\/367"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}