dimanche 7 mai 2023

Why the code below is giving this output?

I want to write a code for printing a rhombus of stars in c Language I have written a code that gives the desired output. :

#include <stdio.h>
int main()
{
    int n, i, j, K;
    printf("Enter no. of rows; ");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n - i + 1; j++)
        {
            printf(" ");
        }
        for (int k = 1; k <= n; k++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

But, I want to get the same result another way. So, I wrote the another code expecting the same result which is below.

I tried this code:

#include <stdio.h>
int main()
{
    int n, a, i, j;
    printf("Enter no. of rows: ");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        for (int j = n - i + 1; 2 * n - i >= j > n - i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

I was expecting the desired output which came for the code written above. But, it resulted in this output. So, I am confused. Why did this happen? Please explain.

Aucun commentaire:

Enregistrer un commentaire