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 ***** * * * * ** * |
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 patterns programs in any programming language. However, what is important is the logic behind patterns programs. You need to thoroughly understand the working of the while loop 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