Program to print diagonal star patterns
Last Updated :
20 Feb, 2023
Time Complexity: O(N) where N is number of nodes in a given binary tree
Auxiliary Space: O(N)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++
#include <iostream>
using namespace std;
void pattern(int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0 || i == j || i == n - 1
|| j == n - 1 || i + j == n - 1)
cout << "*";
else
cout << " ";
}
cout << endl;
}
}
int main()
{
int n = 7;
pattern(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static void pattern(int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
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();
}
}
public static void main(String argc[]){
int n = 7;
pattern(n);
}
}
|
Python3
def pattern(n) :
for i in range(0 , n) :
for j in range(0 , n) :
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("")
n = 7
pattern(n)
|
C#
using System;
public class GfG{
public static void pattern(int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0 || i == j
|| i == n - 1 || j == n - 1
|| i + j == n - 1)
Console.Write("*");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
public static void Main(){
int n = 7;
pattern(n);
}
}
|
PHP
<?php
function pattern($n)
{
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
{
if ($i == 0 || $j == 0 || $i == $j ||
$i == $n - 1 || $j == $n - 1 ||
$i + $j == $n - 1)
echo "*";
else
echo " ";
}
echo "\n";
}
}
$n = 7;
pattern($n);
?>
|
Javascript
<script>
function pattern( n)
{
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (i == 0 || j == 0 || i == j || i == n - 1 || j == n - 1 || i + j == n - 1)
document.write("*");
else
document.write(" ");
}
document.write("<br/>");
}
}
let n = 7;
pattern(n);
</script>
|
Output :
*******
** **
* * * *
* * *
* * * *
** **
*******
Time Complexity: O(n2)
Auxiliary Space: O(1)
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++
#include <iostream>
using namespace std;
void pattern(int n)
{
int c1 = (n - 1) / 2;
int c2 = 3 * n / 2 - 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j == c1 || i - j == c1 || j - i == c1
|| i + j == c2 || i == c1 || j == c1)
cout << "*";
else
cout << " ";
}
cout << endl;
}
}
int main()
{
int n = 9;
pattern(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static void pattern(int n)
{
int c1 = (n - 1) / 2;
int c2 = 3 * n / 2 - 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
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();
}
}
public static void main(String argc[]){
int n = 9;
pattern(n);
}
}
|
Python3
def pattern(n) :
c1 = (n - 1) // 2
c2 = 3 * n // 2 - 1
for i in range(0 , n) :
for j in range(0 , n) :
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("")
n = 9
pattern(n)
|
C#
using System;
class GfG
{
public static void pattern(int n)
{
int c1 = (n - 1) / 2;
int c2 = 3 * n / 2 - 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i + j == c1 || i - j == c1 ||
j - i == c1 || i + j == c2 ||
i == c1 || j == c1)
Console.Write("*");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
public static void Main()
{
int n = 9;
pattern(n);
}
}
|
PHP
<?php
function pattern($n)
{
$c1 = floor(($n - 1) / 2);
$c2 = floor(3 * $n / 2 - 1);
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
{
if ($i + $j == $c1 || $i - $j == $c1 ||
$j - $i == $c1 || $i + $j == $c2 ||
$i == $c1 || $j == $c1)
echo "*";
else
echo " ";
}
echo "\n";
}
}
$n = 9;
pattern($n);
?>
|
Javascript
<script>
function pattern(n) {
var c1 = (n - 1) / 2;
var c2 = parseInt(3 * n / 2 )- 1;
for (var i = 0; i < n; i++) {
for (var j = 0; j < n; j++) {
if (i + j == c1 || i - j == c1 || j - i == c1 || i + j == c2 || i == c1 || j == c1)
document.write(" *");
else
document.write(" ");
}
document.write("<br/>");
}
}
var n = 9;
pattern(n);
</script>
|
Output :
*
* * *
* * *
* * *
* * * * * * * * *
* * *
* * *
* * *
*
Time Complexity: O(n2)
Auxiliary Space: O(1)
Please Login to comment...