Answer the question
In order to leave comments, you need to log in
How to display multiple times the value of the same variable in C?
I started learning C
, there is a test task
and there the task is to display a semi-pyramid
with a height specified by the user,
for example, if the height is 3,
then it should look like this
##
###
####
I'm just starting to learn the language, so my knowledge is at zero
for(int i=0; i < height; i++ )
{
int hashes = i + 2;
int spaces = (height + 1) - hashes;
printf("_ %d", spaces);
printf("# %d\n", hashes);
}
Answer the question
In order to leave comments, you need to log in
This is done in a double loop.
for(int i=0;i<height;i++) {
for(int j=2;j<=height-i;j++) {
cout<<" ";
}
for(int j=2;j<=i+3;j++) {
cout<<"#";
}cout<<"\n";
}
In the inner loop, we output spaces in the amount of height-i, then we output lattices in the amount of i.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question