Coding Practice

Write a C program to generate the following pattern

Generate Pattern

4. Write a C program to generate the following pattern

Sample Output
Enter number of rows
9

9       9
 8     8
  7   7
   6 6
    5
   4 4
  3   3
 2     2
1       1

Process returned 0 (0x0)   execution time : 1.879 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int i, j, k, rows;
 
    printf("Enter number of rows\n");
    scanf("%d", &rows);
    printf("\n");
 
    for(i = rows, k = 1; i >= 1; i--, k++)
    {
        for(j = rows; j >= 1; j--)
        {
            if(i == j || j == k)
            {
                printf("%d", i);
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}
Sample Output
Enter number of rows
5

5   5
 4 4
  3
 2 2
1   1

Process returned 0 (0x0)   execution time : 1.845 s
Press any key to continue.

No comments:

Post a Comment

Change Theme
X