Skip to content

Instantly share code, notes, and snippets.

@pbesra
Last active January 14, 2019 18:57
Show Gist options
  • Save pbesra/ddeebae56bd8dfe71fa80f0f97a2eb4f to your computer and use it in GitHub Desktop.
Save pbesra/ddeebae56bd8dfe71fa80f0f97a2eb4f to your computer and use it in GitHub Desktop.
Tree shape | Pascal triangle using asterisk | C++
#include <iostream>
using namespace std;
int main(){
int n=4;
cout << "enter n: " << endl;
cin >> n;
cout << "result: " << endl;
int start=n-1; // this where * starts in every row.
int t=0;
for(int i=0;i<n;i++){
// this loops creates spaces left side of the triangle
for(int j=0;j<start;j++){
cout << " ";
}
// depending upon row being odd or even, position of asterisk and space changes
if(i%2==0){
for(int j=start;j<=start+(2*i+1);j++){
// even row-odd col contains * and even row-even col contains space
if(j%2==0){
cout << " ";
}else{
cout << "*";
}
}
}else{
// odd row-even col contains * and odd row-odd col contains space
for(int j=start;j<=start+(2*i+1);j++){
if(j%2==0){
cout << "*";
}else{
cout << " ";
}
}
}
start--;
cout << endl;
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment