Probset1_Q2

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.1 kB
1
Indexable
Never
#include<stdio.h>
int main()
{
    int n, i, j, k, p;
    scanf("%d",&n); //INPUT A NUMBER
    if (n <= 0)
    printf("Invalid Number Entered"); // CHECK VALIDITY
    else
    {
        for(i = 1;i <= n;i++)         //PRINT TOP HALF OF THE PATTERN
        {   
            for(k = n-i;k > 0;k--)
            printf(" ");
            
            if (i%2 == 0)
            p = 2*n-1;
            else
            p = 2*n;
            
            for(j = 1;j <= i;j++)
            {
                printf("%d ",p);
                p -= 2;
            }
            printf("\n");
        }
        for(i = n-1;i >= 1;i--)     //PRINT BOTTOM HALF OF THE PATTERN
        {   
            for(k = i;k < n;k++)
            printf(" ");
            
            if (i%2 == 0)
            p = 2*n-1;
            else
            p = 2*n;
            
            for(j = 0;j < i;j++)
            {
                printf("%d ",p);
                p -= 2;
            }
            printf("\n");
        }
        
    }
    return 0;   
}