{"id":7134,"date":"2019-03-10T01:31:28","date_gmt":"2019-03-09T20:01:28","guid":{"rendered":"https:\/\/java2blog.com\/?p=7134"},"modified":"2021-01-11T12:31:49","modified_gmt":"2021-01-11T07:01:49","slug":"print-pyramid-pattern-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/print-pyramid-pattern-java\/","title":{"rendered":"Print pyramid pattern: 1 3*2 4*5*6 pattern in java"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Problem\">Problem<\/a><\/li><li><a href=\"#Solution\">Solution<\/a><\/li><li><a href=\"#Java_program_to_print_1_32_456_pattern_in_java\">Java program to print 1 3*2 4*5*6 pattern in java<\/a><\/li><\/ul><\/div>\n<p>In this post, we will see how to print the following pyramid pattern.<\/p>\n<hr \/>\n<h2><span id=\"Problem\"><span style=\"color: #f89820;\">Problem<\/span><\/span><\/h2>\n<div class=\"content-box-green\">Input : n = 4<br \/>\nOutput :<br \/>\n1<br \/>\n3*2<br \/>\n4*5*6<br \/>\n10*9*8*7Input : n = 5<br \/>\nOutput :<br \/>\n1<br \/>\n3*2<br \/>\n4*5*6<br \/>\n10*9*8*7<br \/>\n11*12*13*14*15<\/div>\n<hr \/>\n<h2><span id=\"Solution\"><span style=\"color: #f89820;\">Solution<\/span><\/span><\/h2>\n<p>If you notice the pattern we need to print odd rows in increasing order and even rows in decreasing order.<br \/>\nWe will use two <a href=\"https:\/\/java2blog.com\/for-loop-java-example\/\" target=\"_blank\" rel=\"noopener\">for loops<\/a> and three <a href=\"https:\/\/java2blog.com\/variables-java\/\" target=\"_blank\" rel=\"noopener\">variables<\/a> to achieve this pattern.<br \/>\nThree variables:<\/p>\n<ul>\n<li><strong>row:<\/strong> It denotes to current row<\/li>\n<li><strong>col:<\/strong> It is the number which you actually prints<\/li>\n<li><strong>num:<\/strong> It actually controls the number upto which you are going to print in a row.<\/li>\n<\/ul>\n<h2><span id=\"Java_program_to_print_1_32_456_pattern_in_java\">Java program to print 1 3*2 4*5*6 pattern in java<\/span><\/h2>\n<pre class=\"java\" name=\"code\">package org.arpit.java2blog;\n\n\/\/ Java implementation to print the \n\/\/ following pyramid pattern \npublic class PyramidPattern { \n\n    \/* method to print the following pyramid \n    \/\/ pattern\n     *  1\n        3*2\n        4*5*6\n        10*9*8*7\n     *\/\n    static void printPattern(int n) \n    { \n        int col, num = 0; \n\n        \/\/ loop for row\n        for (int row = 1; row &lt;= n; row++) { \n\n            \/\/ when row number is odd,then print in increasing order.\n            if (row % 2 != 0) { \n\n                \/\/ printing in ascending order\n                for (col = num + 1; col &lt; num + row; col++) \n                    System.out.print(col + \"*\"); \n                System.out.println(col++); \n\n                \/\/ update value of 'num' \n                num = col; \n            } \n\n         \/\/ when row number is odd,then print in decending order.\n            else { \n\n                \/\/ update value of 'num' \n                num = num + row - 1; \n\n                \/\/ print numbers with the '*' in \n                \/\/ decreasing order \n                for (col = num; col &gt; num - row + 1; col--) \n                    System.out.print(col + \"*\"); \n                System.out.println(col); \n            } \n        } \n    } \n\n    \/\/ Driver program to test above \npublic static void main(String args[]) \n    { \n        int n = 5; \n        printPattern(n); \n    } \n} \n<\/pre>\n<p><a href=\"https:\/\/java2blog.com\/introduction-to-complexity-of-algorithm\/\" target=\"_blank\" rel=\"noopener\">Time Compexity<\/a>: O((n <em> (n + 1)) \/ 2)<br \/>\nThat&#8217;s all about printing the pattern 1 3<\/em>2 4<em>5<\/em>6 pattern in java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsProblemSolutionJava program to print 1 3*2 4*5*6 pattern in java In this post, we will see how to print the following pyramid pattern. Problem Input : n = 4 Output : 1 3*2 4*5*6 10*9*8*7Input : n = 5 Output : 1 3*2 4*5*6 10*9*8*7 11*12*13*14*15 Solution If you notice the pattern we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[151,144],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/7134"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=7134"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/7134\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=7134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=7134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=7134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}