vendredi 20 janvier 2023

C program to create a square pattern with two triangles

So, the output should come out with a incrementing triangle of looped numbers (1,2,3,...) combined with a decrementing triangle of stars. Like this:

1********
12*******
123******
1234*****
12345****
123456***
1234567**
12345678*
123456789

So far I have this but just can't figure out how to decrement the stars.

#include <stdio.h>

int main()
{
    int i, j;
    for(i=1; i<=9; i++)
    {
        for (j=1; j<=i; j++)
        {
            printf ("%d",j);
        }

        int k;
        for(k=8; k>0; k--) {
            printf("*");
        }

        printf("\n");
    }
}

And it prints out this:

1*******
12*******
123*******
1234*******
12345*******
123456*******
1234567*******
12345678*******
123456789*******

Aucun commentaire:

Enregistrer un commentaire