
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Interesting Pattern in C++
In this tutorial, we will be discussing a program to print a given interesting pattern.
For this, we will be provided with the half-width of the pattern. Our task is to print a similar pattern according to the given width with its left and right portions being mirror images of one another.
Example
#include<stdio.h> //printing the given pattern void print_pattern(int n){ int i,j; //printing the upper half for (i=1; i<=n; i++){ for (j=1; j<=(2*n); j++){ // Left portion if (i<j) printf(" "); else printf("*"); // Right portion if (i<=((2*n)-j)) printf(" "); else printf("*"); } printf("\n"); } //printing the lower half for (i=1; i<=n; i++){ for (j=1;j<=(2*n);j++){ // Left portion if (i>(n-j+1)) printf(" "); else printf("*"); // Right portion if ((i+n)>j) printf(" "); else printf("*"); } printf("\n"); } } int main(){ print_pattern(6); return 0; }
Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Advertisements