MarineBiologist
MarineBiologist

Reputation: 55

How do I print this pattern in C?

I have to print this:

          0          
        1 0 1        
      2 1 0 1 2      
    3 2 1 0 1 2 3    
  4 3 2 1 0 1 2 3 4  
5 4 3 2 1 0 1 2 3 4 5

My code:

#include<stdio.h>
    int main()
    {
        int i,j,k,l;

        for(i=1;i<=6;i++)
        {
            for(j=6;j>i;j--)
            {
                printf(" ");
            }

            for(k=i-1;k>=0;k--)
            {
                printf("%d",k);
            }

            for(l=1;l<i;l++)
                printf("%d",l);
            printf("\n");
        }
        return 0;
    }

My output:

     0          
    101        
   21012      
  3210123    
 432101234  
54321012345

I have just started coding in C so this is new to me. How do I put space between numbers so that the final output looks more elegant than it currently is?

I tried %-6d in printf("%d",k); to adjust the width but it had wrong indentation of the output.

Upvotes: 1

Views: 5306

Answers (3)

Durgpal Singh
Durgpal Singh

Reputation: 11983

use this.. add one space in in all three loops.

int i, j, k, l;
for (i = 1; i <= 6; i++) {
    for (j = 6; j > i; j--) {
        System.out.printf("  ");
    }
    for (k = i - 1; k >= 0; k--) {
        System.out.printf("%d ", k);
    }
    for (l = 1; l < i; l++) {
        System.out.printf("%d ", l);
    }
    System.out.printf("\n");
}

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882596

That's easily fixed. Everywhere that you currently print a character (other than the newline), simply print that character followed by a space.

If you do that, you'll see the output you desire:

          0 
        1 0 1 
      2 1 0 1 2 
    3 2 1 0 1 2 3 
  4 3 2 1 0 1 2 3 4 
5 4 3 2 1 0 1 2 3 4 5 

I won't show you the code since it's almost certainly classwork and you'll be a better coder if you nut it out yourself. But I will give you the tidbit that there are three lines that need to change. That should be more than enough to get the issue solved.

As a side note, this will also print a space at the end of each line, after the final digit. If that's not allowed, there are ways to fix it, primarily by changing the final loop so it doesn't output a space for the final item on the line.

It will also start failing miserably if you start trying to use two-digit numbers. To solve that, you need to know the widest number and use printf fields specifiers with widths. However, since it wasn't in the original spec, I haven't implemented that (see YAGNI).


If you wanted to implement something like that (with both the larger numbers and removed space at line end), you could use something like:

#include<stdio.h>

#define COUNT 15

int main(void) {
    // Sanity check.

    if (COUNT < 1) return 0;

    // Get maximum width.

    int width = 1, value = COUNT;
    while (value > 9) {
        value /= 10;
        width++;
    }

    // For each line.

    for (int line = 1; line <= COUNT; line++) {
        // Output leading spaces.

        for (int column = COUNT; column > line; column--)
            printf("%*s ", width, "");

        // Output descending digits.

        for (int column = line - 1; column >= 0; column--)
            printf("%*d ", width, column);

        // Output ascending digits.

        for (int column = 1; column < line; column++) {
            printf("%*d", width, column);
            if (column < line - 1) putchar(' ');
        }

        // Finish off line.

        putchar('\n');
    }

    return 0;
}

It has the following advantages:

  • it allows for variable widths.
  • it uses more appropriately named (and fewer) loop variables.
  • it doesn't output the space at the end of each line.

Just don't use it as is if this is classwork, you'll almost certainly be found out.

Upvotes: 5

Vlad Korotkevich
Vlad Korotkevich

Reputation: 11

Instead of printf("%d",k) use printf("%d ",k).

Upvotes: -1

Related Questions