Program to print diagonal star patterns
For the given input, this program prints the following pattern. The input must be an odd number.
Examples:
Input : 7
Output :
*******
** **
* * * *
* * *
* * * *
** **
*******
Below is the code printing above pattern :
C++
// CPP program to print diagonal star patterns #include <iostream> using namespace std; void pattern(int n) { // Loop denoting rows for (int i = 0; i < n; i++) { // Loop denoting columns for (int j = 0; j < n; j++) { // Checking boundary conditions and main // diagonal and secondary diagonal conditions if (i == 0 || j == 0 || i == j || i == n - 1 || j == n - 1 || i + j == n - 1) cout << "*"; else cout << " "; } cout << endl; } } // Driver code int main() { // n denotes size which should be odd int n = 7; // Function calling pattern(n); return 0; } |
chevron_right
filter_none
Java
// Java program to print diagonal star patterns import java.util.*; import java.lang.*; public class GfG{ public static void pattern(int n) { // Loop denoting rows for (int i = 0; i < n; i++) { // Loop denoting columns for (int j = 0; j < n; j++) { // Checking boundary conditions // and main diagonal and // secondary diagonal conditions if (i == 0 || j == 0 || i == j || i == n - 1 || j == n - 1 || i + j == n - 1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } // Driver function public static void main(String argc[]){ // n denotes size which should be odd int n = 7; // Function calling pattern(n); } } // This code is contributed by Sagar Shukla |
chevron_right
filter_none
Python3
# Python 3 program to print # diagonal star patterns def pattern(n) : # Loop denoting rows for i in range(0 , n) : # Loop denoting columns for j in range(0 , n) : # Checking boundary conditions and main # diagonal and secondary diagonal conditions if (i == 0 or j == 0 or i == j or i == n - 1 or j == n - 1 or i + j == n - 1) : print( "*", end="") else : print(" ",end="") print("") # Driver code # n denotes size which should be odd n = 7 # Function calling pattern(n) # This code is contributed by Nikita Tiwari. |
chevron_right
filter_none
C#
// C# program to print diagonal // star patterns using System; public class GfG{ public static void pattern(int n) { // Loop denoting rows for (int i = 0; i < n; i++) { // Loop denoting columns for (int j = 0; j < n; j++) { // Checking boundary conditions, // main diagonal and secondary // diagonal conditions if (i == 0 || j == 0 || i == j || i == n - 1 || j == n - 1 || i + j == n - 1) Console.Write("*"); else Console.Write(" "); } Console.WriteLine(); } } // Driver function public static void Main(){ // n denotes size which should be odd int n = 7; // Function calling pattern(n); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // php program to print // diagonal star patterns function pattern($n) { // Loop denoting rows for ($i = 0; $i < $n; $i++) { // Loop denoting columns for ($j = 0; $j < $n; $j++) { // Checking boundary conditions // and main diagonal and secondary // diagonal conditions if ($i == 0 || $j == 0 || $i == $j || $i == $n - 1 || $j == $n - 1 || $i + $j == $n - 1) echo "*"; else echo " "; } echo "\n"; } } // Driver code // n denotes size which should be odd $n = 7; // Function calling pattern($n); // This code is contributed by mits ?> |
chevron_right
filter_none
Output :
*******
** **
* * * *
* * *
* * * *
** **
*******
For given input, this program prints the following pattern. The input must be an odd number.
Examples :
Input : 9
Output :
*
* * *
* * *
* * *
* * * * * * * * *
* * *
* * *
* * *
*
Below is the code printing above pattern :
C++
// CPP program to print diagonal pattern #include <iostream> using namespace std; void pattern(int n) { // For printing upper portion int c1 = (n - 1) / 2; // For printing lower portion int c2 = 3 * n / 2 - 1; // Loop denoting rows for (int i = 0; i < n; i++) { // Loop denoting columns for (int j = 0; j < n; j++) { // Checking conditions for printing pattern if (i + j == c1 || i - j == c1 || j - i == c1 || i + j == c2 || i == c1 || j == c1) cout << "*"; else cout << " "; } cout << endl; } } // Driver code int main() { // n denotes size int n = 9; // Function calling pattern(n); return 0; } |
chevron_right
filter_none
Java
// Java program to print diagonal star patterns import java.util.*; import java.lang.*; public class GfG{ public static void pattern(int n) { // For printing upper portion int c1 = (n - 1) / 2; // For printing lower portion int c2 = 3 * n / 2 - 1; // Loop denoting rows for (int i = 0; i < n; i++) { // Loop denoting columns for (int j = 0; j < n; j++) { // Checking conditions for printing // pattern if (i + j == c1 || i - j == c1 || j - i == c1 || i + j == c2 || i == c1 || j == c1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } // Driver function public static void main(String argc[]){ // n denotes size which should be odd int n = 9; // Function calling pattern(n); } } // This code is contributed by Sagar Shukla |
chevron_right
filter_none
Python3
# Python 3 program to print # diagonal pattern def pattern(n) : # For printing upper portion c1 = (n - 1) // 2 # For printing lower portion c2 = 3 * n // 2 - 1 # Loop denoting rows for i in range(0 , n) : # Loop denoting columns for j in range(0 , n) : # Checking conditions for # printing pattern if (i + j == c1 or i - j == c1 or j - i == c1 or i + j == c2 or i == c1 or j == c1) : print( "*",end = "") else : print(" ",end = "") print("") # Driver code # n denotes size n = 9 # Function calling pattern(n) # This code is contributed by Nikita Tiwari. |
chevron_right
filter_none
C#
// C# program to print // diagonal star patterns using System; class GfG { public static void pattern(int n) { // For printing // upper portion int c1 = (n - 1) / 2; // For printing // lower portion int c2 = 3 * n / 2 - 1; // Loop denoting rows for (int i = 0; i < n; i++) { // Loop denoting columns for (int j = 0; j < n; j++) { // Checking conditions for // printing pattern if (i + j == c1 || i - j == c1 || j - i == c1 || i + j == c2 || i == c1 || j == c1) Console.Write("*"); else Console.Write(" "); } Console.WriteLine(); } } // Driver Code public static void Main() { // n denotes size which // should be odd int n = 9; // Function calling pattern(n); } } // This code is contributed by anuj_67. |
chevron_right
filter_none
PHP
<?php // php program to print // diagonal pattern function pattern($n) { // For printing upper portion $c1 = floor(($n - 1) / 2); // For printing lower portion $c2 = floor(3 * $n / 2 - 1); // Loop denoting rows for ($i = 0; $i < $n; $i++) { // Loop denoting columns for ($j = 0; $j < $n; $j++) { // Checking conditions for // printing pattern if ($i + $j == $c1 || $i - $j == $c1 || $j - $i == $c1 || $i + $j == $c2 || $i == $c1 || $j == $c1) echo "*"; else echo " "; } echo "\n"; } } // Driver code // n denotes size $n = 9; // Function calling pattern($n); // This code is contributed by mits ?> |
chevron_right
filter_none
Output :
*
* * *
* * *
* * *
* * * * * * * * *
* * *
* * *
* * *
*
Recommended Posts:
- Program to print hollow rectangle or square star patterns
- Print different star patterns in SQL
- Program to print Hut Star pattern
- Program to print number with star pattern
- Program to print half diamond Number-Star pattern
- Program to print right and left arrow patterns
- Recursive program to print triangular patterns
- Program to print solid and hollow square patterns
- Program to print solid and hollow rhombus patterns
- Python | Print an Inverted Star Pattern
- Print Patterns in PL/SQL
- Programs to print Interesting Patterns
- Program for Arrow Star Pattern
- Print matrix in diagonal pattern
- Programs to print Triangle and Diamond patterns using recursion
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



