Pattern Printing question asked in CGI Coding Round
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 implementation.
C/C++
// 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; } |
Java
// Java program to print the given pattern class GFG { // utility function to print "-" in every // row. This will take care of printing // "-" in the start of every row static void printdashes(int k) { int i; for (i = 1; i <= k; i++) System.out.print("-"); } // function to print the pattern static 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 = 0; 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) System.out.print("*"); else { System.out.print(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) System.out.print("*"); else { if (k == 1) tracker = temp; System.out.print(temp); temp++; } } decrementor += 2; temp = tracker - support; support--; // In every row, the number of dash counts // is increased by 2 dash_counter += 2; System.out.print("\n"); } } // Driver code public static void main(String arg[]) { int n = 3; pattern(n); } } // This code is contributed by Anant Agarwal. |
Python3
# Python program to print # the given pattern # utility function to # print "-" in every # row. This will take # care of printing # "-" in the start of every row def printdashes(k): for i in range(1,k+1): print("-",end="") # function to print the pattern def pattern(n): # variables for vertical left half ''' 1*2*3* --4*5* ----6* ''' dashes = 0 dash_counter = 0 value = 1 # variables for vertical right half ''' 10*11*12 *8*9 7 ''' decrementor = 0 column_decrementor = 0 support = n - 1 temp = ((n * n) + 1) temp1 = (n * 2) - 1 z = temp for i in range(1,n+1): printdashes(dash_counter) # This part will take # care of the vertical # left half of the pattern for j in range(1,(((2 * n) - dash_counter)+1)): # Printing the "*" in even positions if (j % 2 == 0): print("*",end="") else: print(value,end="") value=value+1 # This part will take # care of the vertical # right half of the pattern for k in range(1,((temp1 - decrementor)+1)): # Printing the "*" in even positions if (k % 2 == 0): print("*",end="") else: if (k == 1): tracker = temp print(temp,end="") temp=temp + 1 decrementor =decrementor + 2 temp = tracker - support support=support - 1 # In every row, the number of dash counts # is increased by 2 dash_counter =dash_counter + 2 print("") # driver program n = 3pattern(n) # This code is contributed # by Anant Agarwal. |
C#
// C# program to print the given pattern using System; class GFG { // utility function to print "-" in every // row. This will take care of printing // "-" in the start of every row static void printdashes(int k) { int i; for (i = 1; i <= k; i++) Console.Write("-"); } // function to print the pattern static void pattern(int n) { // variables for vertical left half /* 1*2*3* --4*5* ----6* */ int i, j, dash_counter = 0; int value = 1; // variables for vertical right half /* 10*11*12 *8*9 7 */ int k, decrementor = 0; int support = n - 1; int temp = ((n * n) + 1); int temp1 = (n * 2) - 1; int tracker = 0; 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) Console.Write("*"); else { Console.Write(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) Console.Write("*"); else { if (k == 1) tracker = temp; Console.Write(temp); temp++; } } decrementor += 2; temp = tracker - support; support--; // In every row, the number of dash counts // is increased by 2 dash_counter += 2; Console.WriteLine(); } } // Driver code public static void Main() { int n = 3; pattern(n); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to print the given pattern // function to print the pattern function pattern($n) { // variables for vertical left half $dashes = 0; $dash_counter = 0; $value = 1; // variables for vertical right half $decrementor = 0; $column_decrementor = 0; $support = $n - 1; $temp = (($n * $n) + 1); $temp1 = ($n * 2) - 1; $z = $temp; for ($i = 1; $i <= $n; $i++) { // loop for printing dash for ($dd = 1; $dd <= $dash_counter; $dd++) printf("-"); // 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($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($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 code $n = 3; pattern($n); // This code is contributed by mits ?> |
Output :
1*2*3*10*11*12 --4*5*8*9 ----6*7
Another approach :
C
// C program to print the given pattern #include<stdio.h> // function to print the pattern void printPattern(int row) { int i,j,x = 1,y = row - 1,z; int col = row * ((row + 1) / 2) * ((row + 1) / 2) - 1; // for loop i for(i = 1; i <= row; i++) { // for loop j for(j = 1;j <= col;j++) { if((col-(i + i) + 2) >= j) { if((i + i) - 2 >= j) printf("-"); else { if(j <= col / 2 && j % 2 != 0) { printf("%d",x++); z = x + (y * y) + y; } else if(j > col / 2 && j % 2 != 0) printf("%d", z++); else printf("*"); } } } y--; printf("\n"); } } // Driver code int main() { int row = 3; printPattern(row); return 0; } // This code is contributed by ankurmishra1794 |
Java
// Java program to print the given pattern class GFG { // function to print the pattern static void printPattern(int row) { int i, j, x = 1, y = row - 1, z = 0; int col = row * ((row + 1) / 2) * ((row + 1) / 2) - 1; // for loop i for(i = 1; i <= row; i++) { // for loop j for(j = 1;j <= col;j++) { if((col-(i + i) + 2) >= j) { if((i + i) - 2 >= j) System.out.printf("-"); else { if(j <= col / 2 && j % 2 != 0) { System.out.printf("%d",x++); z = x + (y * y) + y; } else if(j > col / 2 && j % 2 != 0) System.out.printf("%d", z++); else System.out.printf("*"); } } } y--; System.out.printf("\n"); } } // Driver code public static void main(String[] args) { int row = 3; printPattern(row); } } /* This code contributed by PrinciRaj1992 */ |
C#
// C# program to print the given pattern using System; class GFG { // function to print the pattern static void printPattern(int row) { int i, j, x = 1, y = row - 1, z = 0; int col = row * ((row + 1) / 2) * ((row + 1) / 2) - 1; // for loop i for(i = 1; i <= row; i++) { // for loop j for(j = 1;j <= col;j++) { if((col-(i + i) + 2) >= j) { if((i + i) - 2 >= j) Console.Write("-"); else { if(j <= col / 2 && j % 2 != 0) { Console.Write("{0}",x++); z = x + (y * y) + y; } else if(j > col / 2 && j % 2 != 0) Console.Write("{0}", z++); else Console.Write("*"); } } } y--; Console.Write("\n"); } } // Driver code public static void Main(String[] args) { int row = 3; printPattern(row); } } // This code has been contributed by 29AjayKumar |
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.
Recommended Posts:
- Inner reducing pattern printing
- Printing Heart Pattern in C
- Printing Triangle Pattern in Java
- Printing the Triangle Pattern using last term N
- How to learn Pattern printing easily?
- Printing string in plus ‘+’ pattern in the matrix
- Program for Expressionless Face Pattern printing
- What coding habits improve timing in coding contest?
- Programs for printing pyramid patterns in C++
- PHP programs for printing pyramid patterns
- Programs for printing pyramid patterns using recursion
- Programs for printing pyramid patterns in Python
- Programs for printing pyramid patterns in Java
- Different types of Coding Schemes to represent data
- Round the given number to nearest multiple of 10



