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 |
|
|
Code | Output |
|
|
Code | Output |
|
|
Code | Output |
|
|
Code | Output |
|
|
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:
- WAP in C++ & Python to transpose a Matrix
- Files and Streams in C++
- WAP in C++ & Python to find the largest & smallest element in Array
- C++ Pointers
- WAP to find the Sum of Series 1/2+4/5+7/8+…
- Interfaces in C++
- WAP to Find the Sum of Series 1+x+x^2+……+x^n
- C++ Loop Types
- WAP to find the divisors of a Positive Integer
- Data Encapsulation in C++
Leave a Comment on this Post