{"id":355,"date":"2015-11-23T12:56:14","date_gmt":"2015-11-23T01:56:14","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=355"},"modified":"2015-12-06T14:32:36","modified_gmt":"2015-12-06T03:32:36","slug":"printing-triangles-java","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/printing-triangles-java\/","title":{"rendered":"Printing Triangles in Java"},"content":{"rendered":"
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.<\/p>\n
Probably the simplest case is a left aligned triangle of stars that looks like this:<\/p>\n
\r\n*\r\n**\r\n***\r\n****\r\n*****\r\n******\r\n*******\r\n********\r\n*********\r\n**********\r\n<\/pre>\nTo solve this we need to break the problem down. Firstly we identify that we need to print 10 lines, we can use a for loop to achieve this.<\/p>\n
\r\nfor (int i=1; i<=10; i++) {\r\n \/\/ Print a line of the triangle\r\n}\r\n<\/pre>\nThen we need to work out how to print each line. Each line adds one additional asterisk, in fact the number of stars required is represented by the loop variable i. With this information we can use a nested loop to output the required number of stars.<\/p>\n
Here’s the source code for the complete solution to help you understand how it all fits together. Please study the code and let us know if you have any questions.<\/p>\n
Once it all makes sense have a look at the discussion of a few variations of this problem below.<\/p>\n
\r\npackage com.learnjava.math.triangle;\r\n\r\n\/**\r\n * Write a Java program that prints out a triangle\r\n *\r\n * *\r\n * **\r\n * ***\r\n * etc\r\n *\r\n * @author https:\/\/learn-java-by-example.com\r\n *\r\n *\/\r\n\r\npublic class SimpleTriangle {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\t\/\/ Our triangle will have 10 lines\r\n\t\t\/\/ One loop iteration each line\r\n\r\n\t\tfor (int i=1; i<=10; i++) {\r\n\r\n\t\t\t\/\/ For each loop iteration \r\n \/\/ we want to print a number of stars\r\n\t\t\t\/\/ 1st line -> 1 star\r\n\t\t\t\/\/ 2nd line -> 2 star\r\n\t\t\t\/\/ ...\r\n\r\n\t\t\tfor (int j=0;j<i; j++) {\r\n\t\t\t\tSystem.out.print("*");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t}\r\n\t}\r\n}\r\n\r\n<\/pre>\nTurn it upside down<\/h2>\n
What if we want to turn our triangle upside down?<\/p>\n
\r\n******\r\n*****\r\n****\r\n***\r\n**\r\n*\r\n<\/pre>\nWe are still printing the same number of lines, so our outer loop stays the same. What changes is the number of stars that are printed for each line.<\/p>\n
Full source code for the Reverse Triangle example is available for download<\/a> to our members. <\/p>\n
Pyramid<\/h2>\n
A common variation is to display the triangle as a (symmetric) pyramid.<\/p>\n
\r\n *\r\n ***\r\n *****\r\n *******\r\n *********\r\n ***********\r\n *************\r\n ***************\r\n *****************\r\n*******************\r\n<\/pre>\nAgain the logic is very similar, difference being this time for each line we need to print a number of spaces before printing our stars.<\/p>\n
Full source code for the Pyramid example is available for download<\/a> to our members. <\/p>\n
Numbers instead of Stars\/Asterisks<\/h2>\n
Instead of using stars to print our triangle we may be asked to use numbers. This type of problem generally requires you to implement an additional pattern in the numbers used.<\/p>\n
For example using 0s and 1s, alternating what is used to start the line.<\/p>\n
\r\n0\r\n10\r\n010\r\n1010\r\n01010\r\n101010\r\n<\/pre>\nOr counting down numbers for each line, and incrementally decreasing what each line starts with.<\/p>\n
\r\n54321\r\n4321\r\n321\r\n21\r\n1\r\n<\/pre>\n\nFull source code for all the problems discussed above is available for download<\/a> to our members.
\nx<\/span><\/div>\n