I am trying to create a staircase pattern based on an input integer. (Ex:If I put 6 as my input, the output would be as follow):
#
##
###
####
#####
######
the height and the width of the triangle is 6. I have attempted what I thought could replicate this pattern(below):
//first method
void staircase(int n){
std::string mystring;
int j=1;
int k=0;
for (int i=0;i<n;i++){
while(j<(n-1-i)){
mystring+=" ";
j++;
}
while(k<(i+1)){
mystring+="#";
k++;
}
cout<<mystring<<endl;
}
}
//second method
void staircase(int n) {
std::string mystring;
int j=1;
int k=0;
char blank=' ';
char sign='#';
for (int i=0;i<n;i++){
while(j<(n-1-i)){
mystring.push_back(blank);
j++;
}
while(k<(i+1)){
mystring.push_back(sign);
k++;
}
cout<<mystring<<endl;
}
}
both of the method return the output as followed:
#
##
###
####
#####
######
At first, I thought the first while loop got skipped, but it was not the case when I attempt to put a variable inside the first while loop to test the output. Does anyone have an explanation? Am I missing something crucial? It is because this seems like a fairly simple problem, but I couldn't get it to work for the past hour.