Print diagonal star pattern
The diagonal star pattern is a pattern of stars (*) arranged in a diagonal shape. The pattern is formed by placing stars in a way that creates diagonal lines. This article will explain the problem, provide a detailed explanation of the problem statement, present an example, outline the algorithm and pseudocode, and explain the output with the time complexity of the code.
Problem Statement
The problem is to print a diagonal star pattern based on a given size. The size determines the number of rows and columns in the pattern. The pattern consists of diagonal lines made of stars, forming a symmetrical shape.
For example, if the size is 5, the pattern would look like this:
* * * * * * * * * * * * *
The stars form diagonal lines from the top-left corner to the bottom-right corner and from the top-right corner to the bottom-left corner.
Algorithm and Pseudocode
To solve this problem, we can use nested loops to iterate through each cell of the pattern and print a star (*) if it falls on one of the diagonal lines. Here is the algorithm in pseudocode:
1. Start with a function named diagonal_star that takes the size as a parameter. 2. Check if the size is even. If so, return from the function. 3. Calculate the mid point as size divided by 2. 4. Iterate through each row from 0 to size-1. 5. Inside the row loop, iterate through each column from 0 to size-1. 6. Check if the current cell falls on any of the diagonal lines: - If it falls on the first diagonal line (j == mid), or the second diagonal line (i == mid), or the third diagonal line (mid + i == j), or the fourth diagonal line (mid + j == i), or the fifth diagonal line (mid - i == j), or the sixth diagonal line (i > mid && (size - 1) - (i - mid) == j), then print a star (*). - Otherwise, print two spaces to maintain the shape of the pattern. 7. After printing each row, move to the next line. 8. End the function. 9. In the main function, call the diagonal_star function with different sizes to generate different patterns.
Code Solution
//C Program
//Print diagonal star pattern
#include <stdio.h>
void diagonal_star(int size)
{
if (size % 2 == 0)
{
return;
}
printf("\nSize : %d\n\n", size);
int mid = size / 2;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == mid
|| i == mid
|| mid + i == j
|| mid + j == i
|| mid - i == j
|| mid + j == i
|| (i > mid && (size - 1) - (i - mid) == j))
{
printf(" *");
}
else
{
//include double space
printf(" ");
}
}
printf("\n");
}
}
int main()
{
//Simple test
diagonal_star(11);
diagonal_star(7);
diagonal_star(15);
return 0;
}
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
/*
Java Program
Print diagonal star pattern
*/
class MyPattern
{
public void diagonal_star(int size)
{
if (size % 2 == 0)
{
return;
}
System.out.print("\nSize : "+size+"\n\n" );
int mid = size / 2;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == mid
|| i == mid
|| mid + i == j
|| mid + j == i
|| mid - i == j
|| mid + j == i
|| (i > mid && (size - 1) - (i - mid) == j))
{
System.out.print(" *");
}
else
{
//include double space
System.out.print(" ");
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.diagonal_star(11);
obj.diagonal_star(7);
obj.diagonal_star(15);
}
}
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
/*
C++ Program
Print diagonal star pattern
*/
#include<iostream>
using namespace std;
class MyPattern
{
public: void diagonal_star(int size)
{
if (size % 2 == 0)
{
return;
}
cout << "\nSize : " << size << "\n\n";
int mid = size / 2;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == mid
|| i == mid
|| mid + i == j
|| mid + j == i
|| mid - i == j
|| mid + j == i
|| (i > mid && (size - 1) - (i - mid) == j))
{
cout << " *";
}
else
{
cout << " ";
}
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
//Simple test
obj.diagonal_star(11);
obj.diagonal_star(7);
obj.diagonal_star(15);
return 0;
}
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
/*
C# Program
Print diagonal star pattern
*/
using System;
class MyPattern
{
public void diagonal_star(int size)
{
if (size % 2 == 0)
{
return;
}
Console.Write("\nSize : " + size + "\n\n");
int mid = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
//Test case which is printing the value of star
if (j == mid || i == mid || mid + i == j || mid + j == i || mid - i == j || mid + j == i || (i > mid && (size - 1) - (i - mid) == j))
{
Console.Write(" *");
}
else
{
//include double space
Console.Write(" ");
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.diagonal_star(11);
obj.diagonal_star(7);
obj.diagonal_star(15);
}
}
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
<?php
/*
Php Program
Print diagonal star pattern
*/
class MyPattern
{
public function diagonal_star($size)
{
if ($size % 2 == 0)
{
return;
}
echo("\nSize : ". $size ."\n\n");
$mid = intval($size / 2);
for ($i = 0; $i < $size; ++$i)
{
for ($j = 0; $j < $size; ++$j)
{
//Test case which is printing the value of star
if ($j == $mid || $i == $mid || $mid + $i == $j || $mid + $j == $i || $mid - $i == $j || $mid + $j == $i || ($i > $mid && ($size - 1) - ($i - $mid) == $j))
{
echo(" *");
}
else
{
//include double space
echo(" ");
}
}
echo("\n");
}
}
}
function main()
{
$obj = new MyPattern();
//Simple test
$obj->diagonal_star(11);
$obj->diagonal_star(7);
$obj->diagonal_star(15);
}
main();
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
/*
Node Js Program
Print diagonal star pattern
*/
class MyPattern
{
diagonal_star(size)
{
if (size % 2 == 0)
{
return;
}
process.stdout.write("\nSize : " + size + "\n\n");
var mid = parseInt(size / 2);
for (var i = 0; i < size; ++i)
{
for (var j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == mid || i == mid || mid + i == j || mid + j == i || mid - i == j || mid + j == i || (i > mid && (size - 1) - (i - mid) == j))
{
process.stdout.write(" *");
}
else
{
//include double space
process.stdout.write(" ");
}
}
process.stdout.write("\n");
}
}
}
function main(args)
{
var obj = new MyPattern();
//Simple test
obj.diagonal_star(11);
obj.diagonal_star(7);
obj.diagonal_star(15);
}
main();
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
# Python 3 Program
# Print diagonal star pattern
class MyPattern :
def diagonal_star(self, size) :
if (size % 2 == 0) :
return
print("\nSize : ", size ,"\n\n", end = "")
mid = int(size / 2)
i = 0
while (i < size) :
j = 0
while (j < size) :
# Test case which is printing the value of star
if (j == mid or i == mid or mid + i == j or mid + j == i or mid - i == j or mid + j == i or(i > mid and(size - 1) - (i - mid) == j)) :
print(" *", end = "")
else :
print(" ", end = " ")
j += 1
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
# Simple test
obj.diagonal_star(11)
obj.diagonal_star(7)
obj.diagonal_star(15)
if __name__ == "__main__": main()
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
# Ruby Program
# Print diagonal star pattern
class MyPattern
def diagonal_star(size)
if (size % 2 == 0)
return
end
print("\nSize : ", size ,"\n\n")
mid = size / 2
i = 0
while (i < size)
j = 0
while (j < size)
# Test case which is printing the value of star
if (j == mid || i == mid || mid + i == j || mid + j == i || mid - i == j || mid + j == i || (i > mid && (size - 1) - (i - mid) == j))
print(" *")
else
print(" ")
end
j += 1
end
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Simple test
obj.diagonal_star(11)
obj.diagonal_star(7)
obj.diagonal_star(15)
end
main()
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
/*
Scala Program
Print diagonal star pattern
*/
class MyPattern
{
def diagonal_star(size: Int): Unit = {
if (size % 2 == 0)
{
return;
}
print("\nSize : " + size + "\n\n");
var mid: Int = (size / 2).toInt;
var i: Int = 0;
while (i < size)
{
var j: Int = 0;
while (j < size)
{
//Test case which is printing the value of star
if (j == mid || i == mid || mid + i == j || mid + j == i || mid - i == j || mid + j == i || (i > mid && (size - 1) - (i - mid) == j))
{
print(" *");
}
else
{
print(" ");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.diagonal_star(11);
obj.diagonal_star(7);
obj.diagonal_star(15);
}
}
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
/*
Swift Program
Print diagonal star pattern
*/
class MyPattern
{
func diagonal_star(_ size: Int)
{
if (size % 2 == 0)
{
return;
}
print("\nSize : ", size ,"\n\n", terminator: "");
let mid: Int = size / 2;
var i: Int = 0;
while (i < size)
{
var j: Int = 0;
while (j < size)
{
//Test case which is printing the value of star
if (j == mid || i == mid || mid + i == j || mid + j == i || mid - i == j || mid + j == i || (i > mid && (size - 1) - (i - mid) == j))
{
print(" *", terminator: "");
}
else
{
print(" ", terminator: " ");
}
j += 1;
}
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.diagonal_star(11);
obj.diagonal_star(7);
obj.diagonal_star(15);
}
main();
Output
Size : 11
*
* * *
* * *
* * *
* * *
* * * * * * * * * * *
* * *
* * *
* * *
* * *
*
Size : 7
*
* * *
* * *
* * * * * * *
* * *
* * *
*
Size : 15
*
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * * * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
*
Output Explanation and Time Complexity
The output consists of three patterns with different sizes: 11, 7, and 15. Each pattern is printed using the diagonal_star function. The function follows the algorithm and prints the stars (*) in the correct positions to form the diagonal lines.
The time complexity of the code is O(n^2) because it uses nested loops to iterate through each cell of the pattern, where n is the size of the pattern. As the size increases, the number of iterations grows quadratically.
In conclusion, the code successfully generates a diagonal star pattern based on the given size. The pattern is formed by arranging stars (*) in diagonal lines, creating a symmetrical shape. By understanding the problem, algorithm, and pseudocode, you can modify the code to create different variations of the diagonal star pattern.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment