Printing Pattern Using Loops in C language program:

Print a pattern of numbers from  to  as shown below. Each of the numbers is separated by a single space.

                            4 4 4 4 4 4 4  
                            4 3 3 3 3 3 4   
                            4 3 2 2 2 3 4   
                            4 3 2 1 2 3 4   
                            4 3 2 2 2 3 4   
                            4 3 3 3 3 3 4   
                            4 4 4 4 4 4 4   


Solution:

 #include <stdio.h>

#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{

    int n;
    scanf("%d", &n);
int initial_row = n - 1;
int initial_col = 1 - n;

for (int row = initial_row; row > -n; --row) {
    for (int col = initial_col; col < n; ++col) {
        int distance_from_center = (abs(row) > abs(col)) ? abs(row) : abs(col);
        printf("%d ", distance_from_center + 1);
    }
    puts("");
}
    return 0;
}

If you have any doubt ,please let me know

Post a Comment (0)
Previous Post Next Post