-6

How to get the following output in C:

1    2    3    2    1
4    5    6    5    4
7    8    9    8    7
10   11   12   11   10
13   14   15   14   13
2
  • Have you made any attempts? Mar 2, 2013 at 5:20
  • What are your limitations on this assignment? I'm guessing a long printf chain wasn't what you had in mind. If you tried anything yet?
    – Mike
    Mar 2, 2013 at 5:26

2 Answers 2

3

Use printf with a format argument of %-5d. Character by character, it means:

  • %: Introduce a format specifier rather than literal text.
  • -: Left-align.
  • 5: Pad to 5 columns.
  • d: Print an integer in base 10 (decimal)
1

it depends on what your code is.

maybe you wanted to just do

printf("1 2 3 2 1\n 4 5 6 5 4\n7 8 9 8 7\n10 11 12 11 10\n13 14 15 14 13\n")

but here is another way

#include <stdio.h>
int main(void)
{
    int x = 1;
    int a;
    for(int f = 0; f < 4; f++)
    {
        for(a = x; a < x + 2; a++)
            printf("%d ", a);
        x = a;
        for(int a = x; a > x - 3; a--)
            printf("%d ", a);
        printf("\n");
        x++;
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.