Articles

C++ program to print pattern Example 1

C++ program to print pattern Example 1

In this chapter of C++ program tutorial our task is:

  • write a program in C++ to print star pattern

* * * *

* * * *

* * * *

* * * *


C++  pattern printing program Example - 1

Given below is a cpp program to print star patterns as shown above:

//program author : Sayantan Bose
#include<iostream>
using namespace std;
//This the function to print the given pattern
void pattern(int noOfLines)
{
    int i,j;
    for(i=1;i<=noOfLines;i++){       //keep a track of the rows
        for(j=1;j<=noOfLines;j++){   //keep a track of the column
            cout<<"* ";
        }
        cout<<endl;
    }
}
//This is the main function
int main()
{
    int n;
    cout<<"Enter the number of lines you want enter"<<endl;
    cin>>n;
    cout<<"The following is the required pattern"<<endl;
    pattern(n);
    return 0;
}

OUTPUT :

Enter the number of lines you want enter

4
The following is the required pattern
* * * *
* * * *
* * * *
* * * *
 
EXPLANATION :
In the above program:
  • From the main function the user is taking the required number of lines that needs to be printed
  • Then a function "pattern" is called to print the required pattern
  • There are two variables name "i" and "j"
  • "i" keep a track of number of rows
  • "j" keep a track of number of columns

This is also an example of nested loop

You can see the above code execution and output in codeblocks IDE:  

cpp program to print star patterns

 


CPP Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sayantan Bose
A coder who learns from mistakes
Page Views :    Published Date : Oct 03,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!