A
A
Alexander2016-05-02 19:19:55
C++ / C#
Alexander, 2016-05-02 19:19:55

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);
    }

I calculated how many spaces and spaces should be,
but how to make one printf display the required number of characters
of the PRINTF type # * hashes

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sanan Yuzb, 2016-05-02
@alexmixaylov

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";
}

A
alex_ak1, 2016-05-02
@alex_ak1

In the inner loop, we output spaces in the amount of height-i, then we output lattices in the amount of i.

A
Alexey, 2016-05-02
@alsopub

Look here - single loop version - ru.stackoverflow.com/questions/397417/%D0%9A%D0%B0...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question