Sunil
Sunil

Reputation: 43

I want to print c pattern as upper triangle

I want to print some C pattern for upper triagnle which sholud print numeric value 1 to 10. Now my code is like this:-

#include <stdio.h>

int main() {
    int j = 1, k, l, i;
    for (i = 4; i >= 1; i--) {
        for (k = i - 1; k >= 0; k--) {
            printf(" ");
        }
        for (l = 4; l >= i; l--) {
            printf("%2d", j);
            j++;
        }
        printf("\n");
    }
    return 0;
}

Its output is fine but when my value reach near 10 it's space is missing. How can I resolve it?

My output looks like

    1
   2 3
  4 5 6
 7 8 910

It should be:

    1
   2 3
  4 5 6
 7 8 9 10

Upvotes: 0

Views: 274

Answers (4)

chqrlie
chqrlie

Reputation: 144780

For this problem, with n lines of output, you need to produce n - i spaces at the beginning of line i and i numbers each preceded by a space. Instead of "%2d", use " %d" and output one less space at the start of each line. You can use printf to output an arbitrary number of spaces using %*s and an empty string.

Here is a modified version:

#include <stdio.h>

int main() {
    int i;      // line number
    int j = 1;  // starting number
    int n = 4;  // number of lines
    int k;
    for (i = 1; i <= n; i++) {
        printf("%*s", n - i, ""); // output n - i spaces
        for (k = 0; k < i; k++) {
            printf(" %d", j);
            j++;
        }
        printf("\n");
    }
    return 0;
}

Output:

    1
   2 3
  4 5 6
 7 8 9 10

Upvotes: 3

Amongst the other good answers, another way is to use printf("%d ", j); instead of printf("%2d", j); - Note the white space ' ' behind the %d format specifier. - This method has the disadvantage that you have a trailing white space after 10 but it accomplishes the (obvious) desired output:

Online example

#include <stdio.h>

int main (void)
{
    int j = 1, k, l, i;
    for (i = 4; i >= 1; i--){
        for (k = i - 1; k >= 0; k--) {
            printf(" ");
        }
        for(l = 4; l >= i; l--) {
           printf("%d ", j);
           j++;
        }

        printf("\n");
    }
    return 0;
}

Output:

    1 
   2 3 
  4 5 6 
 7 8 9 10 

Upvotes: 1

anastaciu
anastaciu

Reputation: 23802

You can change the alignment by using the - sign. This makes it left-justified.

Replace

printf("%2d",j);

with

printf("%-2d",j);

Live demo

If you want the bottom of the pyramid to be completely to the left with no space you can replace

for (k = i - 1; k >= 0; k--)

with

for (k = i - 1; k > 0; k--)

EDIT:

As @chqrlie pointed out this will leave a trailing blank space in every line except the last one, this can be fixed like:

//...
for (i = 4; i >= 1; i--)
{
    for (k = i - 1; k > 0; k--) // change from k >= 0 to k > 0
    {          
        printf(" ");
    }
    for (l = 4; l >= i; l--)
    {
        printf(" %d", j); // change from "%2d" to " %d"
        j++;
    }
    printf("\n");
}
//...

Live demo

Upvotes: 3

Prajwol Gyawali
Prajwol Gyawali

Reputation: 11

It can be solved in two ways:

One way to resolve this is by simply add spacing (padding) for each character you are printing.

#include<stdio.h>
int main()
{
    int j=1,k,l,i;
    for(i=4;i>=1;i--){
        for(k=i-1;k>=0;k--){
            printf("  ");
        }
        for(l=4;l>=i;l--){
           printf(" %2d ",j); //Padding added
                   ^   ^
           j++;
        }
        printf("\n");
    }
    return 0;
}

Secondly you can change the alignment by using - sign

#include<stdio.h>
int main()
{
    int j=1,k,l,i;
    for(i=4;i>=1;i--){
        for(k=i-1;k>=0;k--){
            printf("  ");
        }
        for(l=4;l>=i;l--){
           printf("-%2d",j); //Left justified
                   ^
           j++;
        }
        printf("\n");
    }
    return 0;
}

Upvotes: 1

Related Questions