Programs to print Interesting Patterns
Program to print following pattern:
Examples :
Input : 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This program is divided into four parts.
C
// C program to print // the given pattern #include<stdio.h> pattern( int n) { int i,j; // This is upper half of pattern for (i=1; i<=n; i++) { for (j=1; j<=(2*n); j++) { // Left part of pattern if (i>(n-j+1)) printf ( " " ); else printf ( "*" ); // Right part of pattern if ((i+n)>j) printf ( " " ); else printf ( "*" ); } printf ( "\n" ); } // This is lower half of pattern for (i=1; i<=n; i++) { for (j=1; j<=(2*n); j++) { // Right Part of pattern if (i<j) printf ( " " ); else printf ( "*" ); // Left Part of pattern if (i<=((2*n)-j)) printf ( " " ); else printf ( "*" ); } printf ( "\n" ); } } // main function int main() { pattern(7); return 0; } |
chevron_right
filter_none
Java
// Java program to print // the given pattern import java.io.*; class GFG { static void pattern( int n) { int i, j; // This is upper half of pattern for (i = 1 ; i <= n; i++) { for (j = 1 ; j <= ( 2 * n); j++) { // Left part of pattern if (i > (n - j + 1 )) System.out.print( " " ); else System.out.print( "*" ); // Right part of pattern if ((i + n) > j) System.out.print( " " ); else System.out.print( "*" ); } System.out.println( "" ); } // This is lower half of pattern for (i = 1 ; i <= n; i++) { for (j = 1 ; j <= ( 2 * n); j++) { // Right Part of pattern if (i < j) System.out.print( " " ); else System.out.print( "*" ); // Left Part of pattern if (i <= (( 2 * n) - j)) System.out.print( " " ); else System.out.print( "*" ); } System.out.println( "" ); } } // main function public static void main(String[] args) { pattern( 7 ); } } // This code is contributed by vt_m |
chevron_right
filter_none
Python3
# Python3 program to print # the given pattern def pattern(n): # This is upper half of pattern for i in range ( 1 , n + 1 ): for j in range ( 1 , 2 * n): # Left part of pattern if i > (n - j + 1 ): print ("", end = ' ' ); else : print ( "*" , end = ''); # Right part of pattern if i + n - 1 > j: print ("", end = ' ' ); else : print ( "*" , end = ''); print (""); # This is lower half of pattern for i in range ( 1 , n + 1 ): for j in range ( 1 , 2 * n): #Left part of pattern if i < j: print ("", end = ' ' ); else : print ( "*" , end = ''); # Right part of pattern if i < 2 * n - j: print ("", end = ' ' ); else : print ( "*" , end = ''); print (""); # Driver Code pattern( 7 ); # This code is contributed by mits |
chevron_right
filter_none
C#
// C# program to print // the given pattern using System; class GFG { static void pattern( int n) { int i, j; // This is upper // half of pattern for (i = 1; i <= n; i++) { for (j = 1; j <= (2 * n); j++) { // Left part of pattern if (i > (n - j + 1)) Console.Write( " " ); else Console.Write( "*" ); // Right part of pattern if ((i + n) > j) Console.Write( " " ); else Console.Write( "*" ); } Console.WriteLine( "" ); } // This is lower // half of pattern for (i = 1; i <= n; i++) { for (j = 1; j <= (2 * n); j++) { // Right Part of pattern if (i < j) Console.Write( " " ); else Console.Write( "*" ); // Left Part of pattern if (i <= ((2 * n) - j)) Console.Write( " " ); else Console.Write( "*" ); } Console.WriteLine( "" ); } } // Driver Code static public void Main () { pattern(7); } } // This code is contributed by ajit |
chevron_right
filter_none
PHP
<?php // PHP program to print // the given pattern function pattern( $n ) { $i ; $j ; // This is upper half of pattern for ( $i = 1; $i <= $n ; $i ++) { for ( $j = 1; $j <= (2 * $n ); $j ++) { // Left part of pattern if ( $i > ( $n - $j + 1)) echo " " ; else echo "*" ; // Right part of pattern if (( $i + $n ) > $j ) echo " " ; else echo "*" ; } printf( "\n" ); } // This is lower half of pattern for ( $i = 1; $i <= $n ; $i ++) { for ( $j = 1; $j <= (2 * $n ); $j ++) { // Right Part of pattern if ( $i < $j ) echo " " ; else echo "*" ; // Left Part of pattern if ( $i <= ((2 * $n ) - $j )) echo " " ; else echo "*" ; } echo "\n" ; } } // Driver Code pattern(7); // This code is contributed by m_kit ?> |
chevron_right
filter_none
Output :
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Program to print following pattern:
Examples :
Input : 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This program is divided into four parts.
C
// C program to print the // given pattern #include<stdio.h> pattern( int n) { int i,j; // This is upper half of pattern for (i=1; i<=n; i++) { for (j=1; j<=(2*n); j++) { // Left part of pattern if (i<j) printf ( " " ); else printf ( "*" ); // Right part of pattern if (i<=((2*n)-j)) printf ( " " ); else printf ( "*" ); } printf ( "\n" ); } // This is lower half of pattern for (i=1; i<=n; i++) { for (j=1;j<=(2*n);j++) { // Left part of pattern if (i>(n-j+1)) printf ( " " ); else printf ( "*" ); // Right part of pattern if ((i+n)>j) printf ( " " ); else printf ( "*" ); } printf ( "\n" ); } } // main function int main() { pattern(7); return 0; } |
chevron_right
filter_none
Java
// Java program to print the // given pattern import java.io.*; class GFG { static void pattern( int n) { int i, j; // This is upper half of pattern for (i = 1 ; i <= n; i++) { for (j = 1 ; j <= ( 2 * n); j++) { // Left part of pattern if (i < j) System.out.print( " " ); else System.out.print( "*" ); // Right part of pattern if (i <= (( 2 * n) - j)) System.out.print( " " ); else System.out.print( "*" ); } System.out.println( "" ); } // This is lower half of pattern for (i = 1 ; i <= n; i++) { for (j = 1 ; j <= ( 2 * n); j++) { // Left part of pattern if (i > (n - j + 1 )) System.out.print( " " ); else System.out.print( "*" ); // Right part of pattern if ((i + n) > j) System.out.print( " " ); else System.out.print( "*" ); } System.out.println( "" ); } } // main function public static void main(String[] args) { pattern( 7 ); } } // This code is contributed by vt_m |
chevron_right
filter_none
Python3
# Python3 program to # print the given pattern def pattern(n): # This is upper # half of pattern for i in range ( 1 , n + 1 ): for j in range ( 1 , 2 * n + 1 ): # Left part of pattern if (i < j): print (" ", end = " "); else : print ( "*" , end = ""); # Right part of pattern if (i < = (( 2 * n) - j)): print (" ", end = " "); else : print ( "*" , end = ""); print (""); # This is lower # half of pattern for i in range ( 1 , n + 1 ): for j in range ( 1 , 2 * n + 1 ): # Left part of pattern if (i > (n - j + 1 )): print (" ", end = " "); else : print ( "*" , end = ""); # Right part of pattern if ((i + n) > j): print (" ", end = " "); else : print ( "*" , end = ""); print (""); # Driver Code pattern( 7 ); # This code is contributed # by mits |
chevron_right
filter_none
C#
// C# program to print // the given pattern using System; class GFG { static void pattern( int n) { int i, j; // This is upper // half of pattern for (i = 1; i <= n; i++) { for (j = 1; j <= (2 * n); j++) { // Left part of pattern if (i < j) Console.Write( " " ); else Console.Write( "*" ); // Right part of pattern if (i <= ((2 * n) - j)) Console.Write( " " ); else Console.Write( "*" ); } Console.WriteLine( "" ); } // This is lower // half of pattern for (i = 1; i <= n; i++) { for (j = 1; j <= (2 * n); j++) { // Left part of pattern if (i > (n - j + 1)) Console.Write( " " ); else Console.Write( "*" ); // Right part of pattern if ((i + n) > j) Console.Write( " " ); else Console.Write( "*" ); } Console.WriteLine( "" ); } } // Driver Code static public void Main () { pattern(7); } } // This code is contributed by ajit |
chevron_right
filter_none
PHP
<?php // PHP program to print // the given pattern function pattern( $n ) { $i ; $j ; // This is upper half // of pattern for ( $i = 1; $i <= $n ; $i ++) { for ( $j = 1; $j <= (2 * $n ); $j ++) { // Left part of pattern if ( $i < $j ) echo " " ; else echo "*" ; // Right part of pattern if ( $i <= ((2 * $n ) - $j )) echo " " ; else echo "*" ; } echo "\n" ; } // This is lower half of pattern for ( $i = 1; $i <= $n ; $i ++) { for ( $j = 1; $j <= (2 * $n ); $j ++) { // Left part of pattern if ( $i > ( $n - $j + 1)) echo " " ; else echo "*" ; // Right part of pattern if (( $i + $n ) > $j ) echo " " ; else echo "*" ; } echo "\n" ; } } // Driver Code pattern(7); // This code is contributed by aj_36 ?> |
chevron_right
filter_none
Output :
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This article is contributed by Aditya Kumar. 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:
- PHP programs for printing pyramid patterns
- Programs for printing pyramid patterns in C++
- Programs for printing pyramid patterns in Java
- Programs for printing pyramid patterns in Python
- Print Patterns in PL/SQL
- Print different star patterns in SQL
- Program to print interesting pattern
- Recursive program to print triangular patterns
- Program to print diagonal star patterns
- Program to print right and left arrow patterns
- Program to print solid and hollow square patterns
- Program to print solid and hollow rhombus patterns
- Program to print hollow rectangle or square star patterns
- Wave Patterns
- C++ bitset interesting facts