Rectangle & Pyramids Pattern in C++

By | October 26, 2019

Here in this article, we have covered some of the important Rectangle and Pyramids patterns using the C programming language. The logic we have used here to create all the patterns is most important when it comes to design 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

#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

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

*****
*  *
* * 
**
*

 

Leave a Reply

Your email address will not be published. Required fields are marked *