Tuesday, 3 June 2014

0

C Program 12: To Print The Given Rectangle Pattern

  • Tuesday, 3 June 2014
  • IndianBooktuber
  • Share
  • Aim: To print this pattern 
    *-***********
    ***-*********
    *****-*******
    *******-*****
    *********-***
    ***********-*
    Code: 

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
         int i, j, k;
         for (i=0; i<=5; i++)
         {
             for (j=1; j<=i+(i+1); j++)
             {
                 printf("*");
             }
            printf("-");
            for (k=1; k<=11-(i+i); k++)
             {
                 printf("*");
             }
             printf("\n"); 
         } 
    getch();         
             

    }


    For this pattern we will need to use three for loops. First loop would be for the number of lines in our pattern. Those are 6 and that's why the condition in the first for loop is i<=5
    Now, we will try to divide our pattern in our two parts - one on the left side of dash and other on the right side.
    The one at left side is printed in series like 1,3,5,7,9,11. So, all we need to do is to try to use a condition on the second for loop which tries to print this series. This is done through the condition j<=i+(i+1) 
    The condition may obviously vary depending upon an individual's logical ability.
    Similarly, in the third for loop, we would try to reverse the series - 11,9,7,5,3,1 And hence we will get the desired pattern.
    Output: 


    In case, you still have doubts.. feel free to contact me. 

    0 Responses to “C Program 12: To Print The Given Rectangle Pattern”

    Post a Comment

    Subscribe