# Print character x times based on user's input

This is a discussion on Print character x times based on user's input within the C Programming forums, part of the General Programming Boards category; Hi! So first of all, please keep in mind that I have just began with C, I took CS50x from ...

1. ## Print character x times based on user's input

Hi!

So first of all, please keep in mind that I have just began with C, I took CS50x from Harvard and so far I have a very limited knowledge, so my question may seem very simple.

What I have to do is to create a "pyramid" which has to look something like this:

Code:
```   #
##
###
####```
The user has to firstly input an integer to declare how many rows the pyramid is going to have.

What I have so far

Code:
```#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
//create an infinite loop that will check the value
while (1)
{
//get an integer from the user which will define the height of the pyramid
printf("Enter the height of the pyramid! The value has to be in range 3 to 24.\n");
int h = GetInt();

//if a value is in the range, then proceed to create a pyramid
if (h<=24 && h >= 3)
{
for(int i = h; i > 0; i--)
{
printf("row\n");
}

break;
}

//otherwise continue loop and ask for an another value
else
{
printf("You have entered the invalid value, please try again.\n");
}
}
return 0;

}```
What I am struggling with currently is to figure out how to print out space (h - 1) times

I would really appreciate any help, but rather then just giving me a finished code I would appreciate a "guidance". Thank you

2. You can't write a loop that repeats something h-1 times and, within the body of that loop, prints a single space?

3. Well I guess I wrote it wrongly. What I am struggling with is to create a loop that will print out space h-1, h-2, h-3 .... h - h times

4. Originally Posted by Kecupochren
I would really appreciate any help, but rather then just giving me a finished code I would appreciate a "guidance". Thank you

Hello there,

What I understand from your problem (well, actually you may need to modify a bit my solution for the value range part inside a loop) is that:

Assuming that numberOfRows is the number of rows:

- For each row N you have to insert numberOfRows - N spaces before start printing sharps

- For each row N you have to print N times the sharp character

- And finally insert a new line character

You can try the following:

Code:
```#include <stdio.h>

int main(int argc, char *argv[])
{
int numberOfRows = 0;
int rowNumber = 0;
int counter = 0;

// start by asking the user the number of rows
printf("Enter the number of rows: ");
scanf("%d",&numberOfRows);

for (rowNumber = 0 ; rowNumber <= numberOfRows ; rowNumber++)
{
// For each row N you have to insert numberOfRows - N spaces
// before start printing sharps
for (counter = 0 ; counter < numberOfRows - rowNumber ; counter++)
printf(" ");

// For each row N you have to print N times the sharp character
for (counter = 0 ; counter < rowNumber ; counter++)
printf("#");

// And finally a new line character for the next loop iteration
printf("\n");
}

return 0;
}```
Which gives the following result (I tested on Linux)

Code:
```\$ gcc -Wall testscript.c -o testscript
\$ ./testscript
Enter the number of rows: 0

\$ ./testscript
Enter the number of rows: 1

#
\$ ./testscript
Enter the number of rows: 2

#
##
\$ ./testscript
Enter the number of rows: 3

#
##
###
\$ ./testscript
Enter the number of rows: 4

#
##
###
####
\$ ./testscript
Enter the number of rows: 5

#
##
###
####
#####
\$```

Regards,
Dariyoosh

5. You can't write a nested loop?

6. Can be done in one loop using variable field width formatting for both the spaces and the number of '#' shown from a predefined array containing 24 '#' (specified as maximum needed) characters.