Articles

C++ program to print star pattern

C++ program to print star pattern


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

  • Write a C++ program to print star pattern given below
   *
  * *
 * * *

* * * *


C++ star pattern printing program

Given below is a c++ program to print star pattern 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--){
        for(j=1;j<=i-1;j++){
            cout<<" ";
        }
        for(k=1;k<=cnt;k++){
            cout<<"* ";
        }
        cout<<endl;
        cnt++;
    }
}
//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;
}
Enter the number of lines you want enter
4
The following is the required pattern
   *
  * *
 * * *
* * * *
 
EXPLANATION for c++ star pattern program:
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

Printing "*" and "* " gives us two different types of pattern

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

C++ program to print pattern example 4


 
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

CPP Programs

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 : Nov 05,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!