
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
Printing Interesting Patterns in C++
This article prints an interesting pattern using C++ programming. Here is the algorithm as following
Algorithm
Step-1 Define the size which will be double automatically Step-2 Print the upper section using a loop Step-3 Print the lower section using a loop
Example
Based on the above algorithm, the following c++ code is carved out as;
#include <iostream> using namespace std; int main(){ int n=3; int i,j; // This is upper half of pattern for (i=1; i<=n; i++){ for (j=1; j<=(2*n); j++){ // Left part of pattern if (i<j) cout<<" "; else cout<<"*"; // Right part of pattern if (i<=((2*n)-j)) cout<<" "; else cout<<"*"; } cout<<endl; } // This is lower half of pattern for (i=1; i<=n; i++){ for (j=1;j<=(2*n);j++){ // Left part of pattern if (i>(n-j+1)) cout<<" "; else cout<<"*"; // Right part of pattern if ((i+n)>j) cout<<" "; else cout<<"*"; } cout<<endl; } return 0; }
After compilation of the above code, the interesting pattern will be printed looks like as.
Output
* * * * * * * * * * * * * * * * * * * * * * * *
Advertisements