Pattern Printing question asked in CGI Coding Round

4.4

Write a program that receives a number as input and prints it in the following format as shown below.
Examples:

Input : n = 3

Output :

1*2*3*10*11*12
--4*5*8*9
----6*7

Input : n = 4

Output :

1*2*3*4*17*18*19*20
--5*6*7*14*15*16
----8*9*12*13
------10*11

Asked in CGI coding round

Approach: The approach is to see the problem not as a single task but three tasks which on combining completes the main task. The three tasks are printing left-half of pattern, printing dashes(-) and printing right-half of pattern. Combining all three tasks we would be able to print the pattern.

left-half of pattern
1*2*3*
--4*5*
----6*

A function printdashes() to print the "-".

right-half of pattern
10*11*12
*8*9
7

Below is the C implementation.

// C program to print the given pattern
#include <stdio.h>

// utility function to print "-" in every
// row. This will take care of printing 
// "-" in the start of every row
void printdashes(int k)
{
    int i;
    for (i = 1; i <= k; i++)
        printf("-");
}

// function to print the pattern
void pattern(int n){
    
    // variables for vertical left half
    /*
    1*2*3*
    --4*5*
    ----6*
    */
    int row, column, dashes = 0;
    int i, j, dash_counter = 0;
    int value = 1;

    // variables for vertical right half
    /*
    10*11*12
    *8*9
    7
    */
    int k, l, decrementor = 0; 
    int column_decrementor = 0;
    int support = n - 1;
    int temp = ((n * n) + 1);
    int temp1 = (n * 2) - 1;
    int z = temp;

    int tracker;

    
    for (i = 1; i <= n; i++) {
        printdashes(dash_counter);
        
        // This part will take care of the vertical
    // left half of the pattern
        for (j = 1; j <= (2 * n) - dash_counter; j++) {
            
            // Printing the "*" in even positions
            if (j % 2 == 0)
                printf("*");
            else {
                printf("%d", value);
                value++;
            }
        }

        // This part will take care of the vertical
        // right half of the pattern
        for (k = 1; k <= (temp1 - decrementor); k++) {
            
            // Printing the "*" in even positions
            if (k % 2 == 0)
                printf("*");
            else {
                if (k == 1)
                    tracker = temp;
                printf("%d", temp);
                temp++;
            }
        }
        decrementor += 2;
        temp = tracker - support;
        support--;

        // In every row, the number of dash counts
        // is increased by 2
        dash_counter += 2;
        printf("\n");
    }
}

// driver program
int main()
{   
    int n = 3;
    pattern(n);
    return 0;
}

Output:

1*2*3*10*11*12
--4*5*8*9
----6*7

This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



4.4 Average Difficulty : 4.4/5.0
Based on 7 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.