Rectangle & Pyramids Pattern in C++

Posted in /  

Rectangle & Pyramids Pattern in C++
vinaykhatri

Vinay Khatri
Last updated on March 29, 2024

    Here in this article, we have covered some of the important Rectangle and Pyramid patterns using the C programming language. The logic we have used here to create all the patterns is most important when it comes to designing a pattern in any language, and with the same logic, we can make those patterns in any other programming language. The logic behind these patterns helps you to understand the logic and working of loops, conditional statements, and print statements.

    Rectangle Pattern in C++

    #include<stdio.h>
    #include <conio.h>
    
    void main()
        {
         int l , b,i,j;
         clrscr();
         printf("Enter the Length of Rectange: ");
         scanf("%d",&l);
         printf("Enter the breadth of Reactangle: " );
         scanf("%d",&b);
    
         for(i=0;i<l;i++)
             {
                for(j=0; j<b ;j++)
                     {  if(i==0||i==l-1||j==0||j==b-1)
                               printf("*");
                          else
                               printf(" ");
                     }
                     printf("\n");
              }
    
         getch();
          }

    Output:

    Enter the Length of Rectangle: 10
    Enter the breadth of Rectangle: 20
    ********************
    *                  *
    *                  *
    *                  *
    *                  *
    *                  *
    *                  *
    *                  *
    *                  *
    ********************

    Pyramids Pattern in C++

    Pyramids Codes Output
    #include<stdio.h>
    #include <conio.h>
    
    void main()
          {
    
            int l ,i,j;
            clrscr();
            printf("Enter the Length of Pyramid: ");
            scanf("%d",&l);
     
            for(i =0;i<l; i++)
               {
                for(j=0; j<=i;j++)
                  {
                     printf("*");
                  }
               printf("\n");
               }
             getch();
            }

    Enter the Length of Pyramid: 6
    
    *
    * *
    * * *
    * * * * 
    * * * * *
    * * * * * *

    Code Output
    #include<stdio.h>
    #include <conio.h>
    void main()
          {
            int l ,i,j;
            clrscr();
            printf("Enter the Length of Pyramid: ");
            scanf("%d",&l);
            for(i =l;i >= 0; i--)
                {
                 for(j=0; j<=i;j++)
                  {
                      printf("*");
                  }
                  printf("\n");
                }
            getch();
          }

    Enter the Length of Pyramid: 6
    
    *  *  *  *  *  *
    *  *  *  *  *
    *  *  *  *
    *  *  *
    *  *
    *

    Code Output
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
     int i, j, l, k = 0;
     printf("Enter the Length of Pyramid: ");
     scanf("%d",&l);
      for(i = 1; i <= l; ++i, k = 0)
      {
        for(j = 1; j <= l – i; ++j)
        {
                printf(" ");
        }
        while(k != 2 * i-1)
          {
             printf("*");
             k++;
             }
        printf("\n");
    }
    getch();
    }

    Enter the Length of Pyramid: 6
    
              *
            *   *
          *   *   *
        *   *   *   *
      *   *   *   *   *
    *   *   *   *   *   *

    Code Output
    #include <stdio.h>
    #include<conio.h>
    void  main()
    {
    int i, j, l,k = 0;
    printf("Enter the Length of Pyramid: ");
    scanf("%d",&l);
    for (i = 1; i <= l; i++)
    {
     for (j = i; j < l; j++) {
       printf(" ");
      }
     while (k != (2 * i - 1)) {
        if (k == 0 || k == 2 * i - 2)
          printf("*");
        else
          printf(" ");
          k++;
          }
        k = 0;
        printf("\n");
        }
    for (i = 0; i < 2 * l - 1; i++) {
    printf("*");
    }
    getch();
    }

    Enter the length of Pyramid: 6
    
             *
            *  *
          *      *
        *          *
      *               *
    *   *   *   *   *   *

    Code Output
    #include <stdio.h>
    #include<conio.h>
    void  main()
    {
    int i, j, l,k = 0;
    printf("Enter the Length of Pyramid: ");
    scanf("%d",&l);
    for (i = 0; i < l; i++)
    {
    printf("*");
    }
    for(i=l; i>=1; –i)
    {
    for(j=0; j < l-i; ++j)
    while (k != (2 * i – 1)) {
    if (k == 0 || k == 2 * i – 2)
    printf("*");
    else
    printf(" ");
    k++;
    }
    k = 0;
    printf(“\n”);
    }
    }

    Enter the Length of Pyramid: 6
    
    *****
    *  *
    * * 
    **
    *

    Conclusion

    While you attend any programming interview or appear for a recruitment drive examination of a tech company, you may likely come across questions related to pattern programs. Also, it is important to know that you can write pattern programs in any programming language. However, what is important is the logic behind pattern programs.

    You need to understand the working of the while loop thoroughly and for loop before writing pattern programs. In this article, we have provided C++ code for rectangle and pyramid patterns with their outputs. Moreover, you can even develop these patterns in other programming languages. Happy coding!

    People are also reading:

    Leave a Comment on this Post

    0 Comments