Given an integer n, the task is to print half-diamond-star pattern.
Examples:
Input: n = 3
Output:
*
**
***
**
*
Input: n = 6
Output:
*
**
***
****
*****
******
*****
****
***
**
*
Nested Loops -O(n^2) Time and O(1) Space
The pattern can be divided into two parts: the upper half and the lower half. We can print each half separately using loops.
- Upper half: Print stars in increasing order. The first row contains 1 star, the second row contains 2 stars, and so on. Therefore, the ith row contains i stars.
- Lower half: Print stars in decreasing order. Each row contains one star fewer than the previous row. Therefore, the ith row contains n - i stars.
using namespace std;
// Function to print the
// half diamond star pattern
void halfDiamondStar(int n)
{
int i, j;
// Loop to print the upper half
// diamond pattern
for (i = 0; i < n; i++) {
for (j = 0; j < i + 1; j++)
cout << "*";
cout << "\n";
}
// Loop to print the lower half
// diamond pattern
for (i = 1; i < n; i++) {
for (j = i; j < n; j++)
cout << "*";
cout << "\n";
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
halfDiamondStar(n);
}
class GFG{
// Function to print the
// half diamond star pattern
static void halfDiamondStar(int n)
{
int i, j;
// Loop to print the upper half
// diamond pattern
for (i = 0; i < n; i++)
{
for (j = 0; j < i + 1; j++)
System.out.print("*");
System.out.print("\n");
}
// Loop to print the lower half
// diamond pattern
for (i = 1; i < n; i++)
{
for (j = i; j < n; j++)
System.out.print("*");
System.out.print("\n");
}
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
// Function Call
halfDiamondStar(n);
}
}
// This code is contributed by Rohit_ranjan
def halfDiamondStar(n):
# Loop to print the upper half
# diamond pattern
for i in range(n):
for j in range(0, i + 1):
print("*", end = "")
print()
# Loop to print the lower half
# diamond pattern
for i in range(1, n):
for j in range(i, n):
print("*", end = "")
print()
# Driver Code
n = 5
# Function Call
halfDiamondStar(n)
using System;
class GFG{
// Function to print the
// half diamond star pattern
static void halfDiamondStar(int n)
{
int i, j;
// Loop to print the upper half
// diamond pattern
for(i = 0; i < n; i++)
{
for(j = 0; j < i + 1; j++)
Console.Write("*");
Console.Write("\n");
}
// Loop to print the lower half
// diamond pattern
for(i = 1; i < n; i++)
{
for(j = i; j < n; j++)
Console.Write("*");
Console.Write("\n");
}
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
// Function Call
halfDiamondStar(n);
}
}
// This code is contributed by Rohit_ranjan
// Function to print the
// half diamond star pattern
function halfdiamondstar(n)
{
let i, j;
// Loop to print the upper half
// diamond pattern
for (i = 0; i < n; i++) {
for (j = 0; j < i + 1; j++)
process.stdout.write("*");
console.log();
}
// Loop to print the lower half
// diamond pattern
for (i = 1; i < n; i++) {
for (j = i; j < n; j++)
process.stdout.write("*");
console.log();
}
}
// driver code
let n = 5;
// Function Call
halfdiamondstar(n);
Output
* ** *** **** ***** **** *** ** *