0

I need to make a program that prints square with x width and y height with #'s. how can I print multiple #'s ?

{
    while (ysize > 0)

    {
        printf("%0*%d\n", xsize,0);
        ysize--;
    }

}

this prints multiple 0's but how i make it so it prints multiple #'s?

E: got it working thanks for helping... answer was:

    while(ysize >0)
{
int i;
for(i=0;i<xsize;i++){
    putchar('#');
}
putchar('\n');
ysize--;

}

user3716050
  • 13
  • 1
  • 3
  • [possible duplicate](http://stackoverflow.com/q/14678948/1841194) – frogatto Jun 06 '14 at 18:05
  • What do you mean "how can I print multiple #'s"? It's really not clear here. Do you want some number of '0's? Do you want exactly N counts of different numbers? Please clarify what you're asking for. – Avery Jun 06 '14 at 18:14

3 Answers3

2

You can, use a function which prints out a line of # of size x, and call that function y times.

void printline(int size){
    int i;
    for(i=0;i<size;i++){
        putchar('#');
    }
    putchar('\n');
}

This function prints x #s in a line and then moves the cursor to the next line.

alexpq
  • 36
  • 3
1

I think you have an extra % in your format string.

This:

printf("%0*d\n", xsize, 0);

should print xsize 0s. This feature exists because it's useful to print numbers padded with leading zeros to a specified width. You can also pad with leading spaces, because that's another commonly desired numeric output format.

There is no such feature built into printf to print multiple # characters.

You'll just have to write a loop to print the # characters one at a time. Or you can build a string containing the # characters and print the whole string, but the loop is probably easier.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

You don't specify if what you want is a filled square or not, so the code example bellow print both.

#include <stdio.h>

int main()
{
    char SHARP = '#';
    int x = 10, y = 10; 

    /* For printing a filled sqaure */

    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
            printf("#");
        printf("\n");
    }


    /* For printing a non-filled squere */
    printf("\n\n"); // Just adding tow lines between sqares.

    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        if (j == 0 || j == y - 1 || i == 0 || i == x - 1)
            printf("#");
        else
            printf(" ");
        printf("\n");
    }

    return 0;
}

The ouput will be:

##########
##########
##########
##########
##########
##########
##########
##########
##########
##########


##########
#        #
#        #
#        #
#        #
#        #
#        #
#        #
#        #
##########
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60