-2

I want to use printf and for loop to print a character multiple times per line depending on the input; i.e. if the input is 3 i want to print:

a
aa
aaa

this is the loop, which doesn't work at all.

for (int i = 0; i < n; i++)
{
    printf("a", i);
    printf("\n");
}

I just don't understand how to print it multiple times on a single line.

yanabeca
  • 35
  • 1
  • 1
  • 7
  • 2
    What do you expect `printf("a", i);` to do *and why*? –  Jan 03 '17 at 00:04
  • You finish the line for every character. \n is synonymous with end of line. – Paul Stelian Jan 03 '17 at 00:06
  • 2
    You will have to have another loop in that loop (`for n = 0; n – Lupe Jan 03 '17 at 00:06
  • Also, do you by chance come from a Python environment? – Paul Stelian Jan 03 '17 at 00:06
  • 1
    #include int main(){ int i, j; int n = 3; for (i=0; i < n; i++){ for (int j = 0; j < i+1; j++){ printf("a"); } printf("\n"); } return 0; } – eyllanesc Jan 03 '17 at 00:08
  • well in my mind assuming n is 3 in the loop i will be 0, 1 and 2. when i is 0 i want to print a, when i is 1 i want to print aa and when i is 2 i want to print aaa and so on n times. and no i don't come from anywhere i assume. i just started learning a couple days ago and this was an exercise. – yanabeca Jan 03 '17 at 00:09
  • @eyllanesc Ow.. – Lupe Jan 03 '17 at 00:09
  • @Lupe :P :P :P :P – eyllanesc Jan 03 '17 at 00:13
  • 2
    The easiest/clearest thing to do is change the first `printf` to a function call `printCharacters(char,int)` or something, and implement it `void printCharacters(char printedChar, int noOfPrints){ for( int i = 0; i < noOfPrints' ++i ) printf("%c", printedChar); }` NOTE: call the function to take a char i.e. `printCharacters('a',i+1);` – George Jan 03 '17 at 00:20
  • @George has a very strange understanding of "easiest/clearest thing to do", considering that exact functionality already exists in `printf` (see my answer) – abelenky Jan 03 '17 at 01:18
  • @abelenky alright then, the answer is the "easiest"(shortest really) thing to do, but the clearest? I wonder how many people know of that functionality and if it was an intended solution to the op's exercise(which would normally be fine it wasn't only it's probable the exercise was to get the op to use a nested loop). – George Jan 03 '17 at 02:10

2 Answers2

2

Use two nested for loops:

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

As suggested in one of the comments in this answer and as per the discussion in this link, putchar is faster than printf if you are printing only one character. So if you are ok with using putchar instead of printf, try the following code instead:

char ch = 'a', newLine = '\n';
for (int i = 0; i < n; i++)
{
    for(int j=0;j<=i;j++) {
       putchar(ch);
    }
    putchar(newLine);
}
Community
  • 1
  • 1
VHS
  • 9,534
  • 3
  • 19
  • 43
  • Even better, use putchar instead of printf. – rici Jan 03 '17 at 00:48
  • Yeah, but I think the OP has stated that he wants to use `for` and `printf` (only). – VHS Jan 03 '17 at 00:52
  • 2
    That could be because OP is unaware that there is a better alternative. To say nothing of future readers of this question who might also appreciate the advice. – rici Jan 03 '17 at 00:55
  • Cool. Updated the answer to include the `putchar` way too. Thanks for your suggestion. – VHS Jan 03 '17 at 01:06
  • This worked perfectly fine, but a small addition. If i want to do the opposite as in have the max number of characters on top minimum on the bottom, what do i need to change in the loop? I tried to make j my max possible input which was 20, and then made it j-- instead of j++ but that seemed to create an infinite loop which crashed the program. Thanks for the answer btw. – yanabeca Jan 03 '17 at 11:30
  • OK nevermind i figured it out. I used `for (int k=n-1; k>=i; k--)` and it worked. – yanabeca Jan 03 '17 at 12:00
2

Unless you need to use a for-loop, I think this would be better for many cases:

printf("%.*s\n", 5, "aaaaaaaaaaaaaaaaaaaaaaaaa"); 

Which will print the first 5 letters of the string (which happens to be more than enough a's)

Put into a complete program:

int main() {
    int i;
    for(i=0; i < 10; ++i)
        printf("%.*s\n", i+1, "aaaaaaaaaaaaaaaaaaa");
    return 0;
}

Output:

a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • 1
    I learned something! I am going to read an implementation or two to see its viability with longer strings but very cool. – Lupe Jan 03 '17 at 09:52