C++ program to print pattern Example 3
In this chapter of C++ program tutorial our task is:
-
write a program in c++ to print star pattern
*
**
***
****
C++ pattern printing program Example 3
Given below is a c++ 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,k,cnt=1;
for(i=noOfLines;i>=1;i--){ //keep a track of the rows
for(j=1;j<=i-1;j++){ //keep a track of the number of spaces to be inserted
cout<<" ";
}
for(k=1;k<=cnt;k++){ //keep a track of the columns
cout<<"*";
}
cout<<endl;
cnt++; //increase the number of stars
}
}
//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 for star pattern programs:
In the above c++ star pattern programs:
-
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 spaces to be inserted
-
"k" keep a track of the columns
-
"cnt" increases the star count
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 08,2020