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:
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Sayantan Bose
- š Iām currently working on DS Algo skills
- š± Iām currently learning web develeopement
- šÆ Iām looking to collaborate with Oppia
- š« Reach me at https://www.linkedin.com/in/sayantan-bose-14134a1a6/
- š Pronouns: his/him
Page Views :
Published Date :
Oct 03,2020