Diamond pattern printing using stars | Diamond star patterns | faceprep

Diamond pattern printing using stars | Diamond star patterns is discussed here. Given a number n, write a program to print a diamond shape with 2*n rows.

Solid Diamond pattern printing using stars

Input: 5

Output:

        *
      *   * 
    *   *   *
  *   *   *   *   
*   *   *   *   *   
*   *   *   *   *
  *   *   *   *
    *   *   *   
      *   * 
        *

Program for solid diamond pattern using stars 

/* C program – solid diamond pattern printing using stars */
#include <stdio.h>

int main()
{
int n, c, k, space = 1;

printf(“Enter the number of rows\n”);
scanf(“%d”, &n);

space = n – 1;

for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(” “);

space–;

for (c = 1; c <= 2*k-1; c++)
printf(“*”);

printf(“\n”);
}

space = 1;

for (k = 1; k <= n – 1; k++)
{
for (c = 1; c <= space; c++)
printf(” “);

space++;

for (c = 1 ; c <= 2*(n-k)-1; c++)
printf(“*”);

printf(“\n”);
}

return 0;
}

/* C++ program – solid diamond pattern printing using stars */
#include <iostream>
using namespace std;

int main()
{
int n, c, k, space = 1;

cout << “\nEnter the number of rows : “;
cin >> n;

space = n – 1;

for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
cout << ” “;

space–;

for (c = 1; c <= 2*k-1; c++)
cout << “*”;

cout << endl;
}

space = 1;

for (k = 1; k <= n – 1; k++)
{
for (c = 1; c <= space; c++)
cout << ” “;

space++;

for (c = 1 ; c <= 2*(n-k)-1; c++)
cout << “*”;

cout << endl;
}

return 0;
}

/* Java program – solid diamond pattern printing using stars */
import java.util.*;

public class Main
{

public static void main(String[] args)
{

int n, c, k, space = 1;
System.out.print(“\nEnter the number of rows : “);
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println();
space = n – 1;

for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
System.out.print(” “);

space–;

for (c = 1; c <= 2*k-1; c++)
System.out.print(“*”);

System.out.print(“\n”);
}

space = 1;

for (k = 1; k <= n – 1; k++)
{
for (c = 1; c <= space; c++)
System.out.print(” “);

space++;

for (c = 1 ; c <= 2*(n-k)-1; c++)
System.out.print(“*”);

System.out.print(“\n”);
}

}
}

# Python program – solid diamond pattern printing using stars
def Diamond_pattern(rows):
n = 0
for i in range(1, rows + 1):
for j in range (1, (rows – i) + 1):
print(end = ” “)

while n != (2 * i – 1):
print(“*”, end = “”)
n = n + 1
n = 0

print()

k = 1
n = 1
for i in range(1, rows):
for j in range (1, k + 1):
print(end = ” “)
k = k + 1

while n <= (2 * (rows – i) – 1):
print(“*”, end = “”)
n = n + 1
n = 1
print()

rows = int(input(“Enter the number of rows : “))
Diamond_pattern(rows)

Output:

solid diamond pattern printing using stars

Hollow diamond pattern printing using stars

Input: 5

Output:

        *
      *   * 
    *       *
  *           *   
*               *   
*               *
  *           *
    *       *   
      *   * 
        *

Program for hollow diamond pattern using stars 

/* C program – hollow diamond pattern printing using stars */

#include <stdio.h>

int main()
{
int i, j, space, k = 0, n;
printf(“\nEnter the number of rows : “);
scanf(“%d”,&n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n – i; j++)
{
printf(” “);
}
while (k != (2 * i – 1))
{
if (k == 0 or k == 2 * i – 2)
printf(“*”);
else
printf(” “);
k++;
}
k = 0;
printf(“\n”);
}
n–;
for (i = n; i >= 1; i–)
{
for (j = 0; j <= n – i; j++)
{
printf(” “);
}
k = 0;
while (k != (2 * i – 1))
{
if (k == 0 or k == 2 * i – 2)
printf(“*”);
else
printf(” “);
k++;
}
printf(“\n”);
}
}

/* C++ program – hollow diamond pattern printing using stars */

#include <iostream>
using namespace std;

int main()
{
int i, j, space, k = 0, n;
cout << “\nEnter the number of rows : “;
cin >> n;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n – i; j++)
{
cout << ” “;
}
while (k != (2 * i – 1))
{
if (k == 0 or k == 2 * i – 2)
cout << “*”;
else
cout << ” “;
k++;
}
k = 0;
cout << endl;
}
n–;
for (i = n; i >= 1; i–)
{
for (j = 0; j <= n – i; j++)
{
cout << ” “;
}
k = 0;
while (k != (2 * i – 1))
{
if (k == 0 or k == 2 * i – 2)
cout << “*”;
else
cout << ” “;
k++;
}
cout << endl;
}
}

/* Java program – hollow diamond pattern printing using stars */
import java.util.*;

public class Main
{

public static void main(String[] args)
{

int i, j, space, k = 0, n;
System.out.print(“\nEnter the number of rows : “);
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println();

for (i = 1; i <= n; i++)
{
for (j = 1; j <= n – i; j++)
{
System.out.print(” “);
}
while (k != (2 * i – 1))
{
if ((k == 0) || (k == 2 * i – 2))
System.out.print(“*”);
else
System.out.print(” “);
k++;
}
k = 0;
System.out.print(“\n”);
}
n–;
for (i = n; i >= 1; i–)
{
for (j = 0; j <= n – i; j++)
{
System.out.print(” “);
}
k = 0;
while (k != (2 * i – 1))
{
if ((k == 0) || (k == 2 * i – 2))
System.out.print(“*”);
else
System.out.print(” “);
k++;
}
System.out.print(“\n”);
}
}
}

# Python program – hollow diamond pattern printing using stars

def Diamond_pattern(n) :
k = 0;

for i in range(1,n+1) :

for j in range(1,n-i+1) :
print(” “,end=””)

while (k != (2 * i – 1)) :
if (k == 0 or k == 2 * i – 2) :
print(“*”,end=””)
else :
print(” “,end=””)
k = k + 1

k = 0

print(“”),

n = n – 1

for i in range (n,0,-1) :
for j in range(0,n-i+1) :
print(” “,end=””)

k = 0
while (k != (2 * i – 1)) :
if (k == 0 or k == 2 * i – 2) :
print(“*”,end=””)
else :
print(” “,end=””)
k = k + 1

print(“”),

n = int(input(“Enter the number of rows : “))
Diamond_pattern(n)

Output:

hollow diamond pattern using stars

Solid Half Diamond pattern printing using stars

Input: 5

Output:

       
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Program for solid half diamond pattern printing using stars

/* C program – solid half diamond pattern printing using stars */

#include

int main()
{
int i, j, space, k = 0, n;
printf(“\nEnter the number of rows : “);
scanf(“%d”,&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
printf(” “);
}
for(int j=1;j<=i;j++)
{
printf(“*”);
}

printf(“\n”);
}

for(int i=n-1;i>0;i–)
{
for(int j=1;j<=n-i;j++)
{
printf(” “);
}
for(int j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
}

/* C++ program – solid half diamond pattern printing using stars */

#include <iostream>
using namespace std;

int main()
{
int i, j, space, k = 0, n;
cout << “\nEnter the number of rows : “;
cin >> n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
cout << ” “;
}
for(int j=1;j<=i;j++)
{
cout << “*”;
}

cout << endl;
}

for(int i=n-1;i>0;i–)
{
for(int j=1;j<=n-i;j++)
{
cout << ” “;
}
for(int j=1;j<=i;j++)
{
cout << “*”;
}
cout << endl;
}
}

/* Java program – solid half diamond pattern printing using stars */
import java.util.*;

public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter N : “);
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(” “);
}
for(int j=1;j<=i;j++)
{
System.out.print(“*”);
}

System.out.println();
}

for(int i=n-1;i>0;i–)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(” “);
}
for(int j=1;j<=i;j++)
{
System.out.print(“*”);
}
System.out.println();
}
}
}

Output:

half diamond pattern

Half Diamond pattern using stars and numbers in a palindromic pattern

Input: 3 (Number of rows)

Output:

*
*  1  *
*  1   2   1  *
*  1   2   3   2   1  *
*  1   2   1   *
*  1  *
*

Program for palindromic pattern printing in a half diamond

#include <stdio.h>

int main()
{
int i, j, N;

printf(“Enter the number of rows : “);
scanf(“%d”, &N);
printf(“*\n”);

for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
if(j == 1)
printf(“*”);
printf(“%d”, j);
}

for(j=i-1; j>=1; j–)
{
printf(“%d”, j);
}
printf(“*”);
printf(“\n”);
}

for(i=N-1; i>=1; i–)
{
for(j=1; j<=i; j++)
{
if(j == 1)
printf(“*”);
printf(“%d”, j);
}

for(j=i-1; j>=1; j–)
{
printf(“%d”, j);
}
printf(“*”);

printf(“\n”);
}
printf(“*\n”);
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int i, j, N;

cout << “Enter the number of rows : “;
cin >> N;
cout << “*\n”;

for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
if(j == 1)
cout << “*”;
cout << j;
}

for(j=i-1; j>=1; j–)
{
cout << j;
}
cout << “*”;
cout << “\n”;
}

for(i=N-1; i>=1; i–)
{
for(j=1; j<=i; j++)
{
if(j == 1)
cout << “*”;
cout << j;
}

for(j=i-1; j>=1; j–)
{
cout << j ;
}
cout << “*”;

cout << “\n”;
}
cout << “*\n”;
return 0;
}

/* Java program for Palindrome pyramid pattern printing using numbers */
import java.util.*;
public class Main {

public static void main(String[] args) {
int N, i, j;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the number of rows : “);
System.out.println();
N = sc.nextInt();
System.out.print(“*\n”);
for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
if(j == 1)
System.out.print(“*”);
System.out.print(j);
}

for(j=i-1; j>=1; j–)
{
System.out.print(j);
}
System.out.print(“*”);
System.out.print(“\n”);
}

for(i=N-1; i>=1; i–)
{
for(j=1; j<=i; j++)
{
if(j == 1)
System.out.print(“*”);
System.out.print(j);
}

for(j=i-1; j>=1; j–)
{
System.out.print(j);
}
System.out.print(“*”);

System.out.print(“\n”);
}
System.out.print(“*\n”);
}
}

N = int(input(“\nEnter the number of rows : “))
print()
print(“*”)
for i in range(1,N+1):
for j in range(1, i+1):
if(j == 1):
print(“*”, end = “”)
print(j,end = “”)

for j in range(i-1, 0, -1):
print(j,end = “”)
print(“*”)

for i in range(N-1, 0, -1):

for j in range(1, i+1):

if(j == 1):
print(“*”,end = “”)
print(j,end = “”)

for j in range(i-1, 0, -1):
print(j,end = “”)
print(“*”)

print(“*\n”);

Output:

palindromic half diamond pattern