Logic to print the given number pattern
11111
 2222
  333
   44
    5

C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)

2304

Program:

<span class="com">/**
 * C program to print number pattern
 */</span><span class="pln">

</span><span class="com">#include</span><span class="pln"> </span><span class="str">&lt;stdio.h&gt;</span><span class="pln">

</span><span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">,</span><span class="pln"> j</span><span class="pun">,</span><span class="pln"> N</span><span class="pun">;</span><span class="pln">

    printf</span><span class="pun">(</span><span class="str">"Enter N: "</span><span class="pun">);</span><span class="pln">
    scanf</span><span class="pun">(</span><span class="str">"%d"</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">N</span><span class="pun">);</span><span class="pln">

    </span><span class="kwd">for</span><span class="pun">(</span><span class="pln">i</span><span class="pun">=</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;=</span><span class="pln">N</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
        </span><span class="com">// Logic to print spaces</span><span class="pln">
        </span><span class="kwd">for</span><span class="pun">(</span><span class="pln">j</span><span class="pun">=</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">&lt;</span><span class="pln">i</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">++)</span><span class="pln">
        </span><span class="pun">{</span><span class="pln">
            printf</span><span class="pun">(</span><span class="str">" "</span><span class="pun">);</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">

        </span><span class="com">// Logic to print numbers</span><span class="pln">
        </span><span class="kwd">for</span><span class="pun">(</span><span class="pln">j</span><span class="pun">=</span><span class="pln">i</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">&lt;=</span><span class="pln">N</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">++)</span><span class="pln">
        </span><span class="pun">{</span><span class="pln">
            printf</span><span class="pun">(</span><span class="str">"%d"</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">);</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">

        printf</span><span class="pun">(</span><span class="str">"\n"</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">

    </span><span class="kwd">return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span>

Output:

11111
 2222
  333
   44
    5

Explanation:

Logic to print the given number pattern

Now, once you get the logic of printing the previous number pattern you can easily print this pattern. As both are similar except that it contains extra trailing spaces. Hence you only need to add the logic of printing spaces before the number gets printed in the first number pattern. If you point your mouse over the pattern you can actually count total number of spaces per row and can get the logic in which spaces are printed. Now as you can see, each row contains row_number - 1 spaces. Logic to print spaces is-

  1. To print spaces, run an inner loop from 1 to current_row_number - 1. Inside this loop print single blank space.

Lets now write down its code.


This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.