Print Hollow Diamond Pattern in C / C++ / Java / Python / C#

Hollow diamond pattern

Programs to print the hollow diamond pattern has been given here.






1. Program & output to print the hollow diamond pattern




1.1. C Program & output to print the hollow diamond pattern

Code has been copied
/********************************************
  	     alphabetacoder.com
C program to print the hollow diamond pattern
*********************************************/

#include <stdio.h>

int main() {
    // declare variables
    int row, i, j, k;

    // take input
    printf("Enter the number of rows: ");
    scanf("%d", & row);

    // new line
    printf("\n");

    // display the pattern
    // display the upper half
    for (i = 1; i <= row; i++) {
        // print spaces
        for (j = 1; j <= row - i; j++) {
            printf(" ");
        }

        for (k = 1; k <= (2 * i - 1); k++) {
            if (k == 1 || k == (2 * i - 1))
                printf("*");
            else
                printf(" ");
        }
        // new line
        printf("\n");
    }

    // display the lower half
    for (i = 1; i <= row - 1; i++) {
        // print spaces
        for (j = 1; j <= i; j++) {
            printf(" ");
        }

        for (k = 1; k <= (2 * (row - i) - 1); k++) {
            if (k == 1 || k == (2 * (row - i) - 1))
                printf("*");
            else
                printf(" ");
        }
        // new line
        printf("\n");
    }

    return 0;
}

Output


Enter the number of rows: 8


       *

      * *

     *   *

    *     *

   *       *

  *         *

 *           *

*             *

 *           *

  *         *

   *       *

    *     *

     *   *

      * *

       *




1.2. C++ Program & output to print the hollow diamond pattern

Code has been copied
/**********************************************
  	     alphabetacoder.com
C++ program to print the hollow diamond pattern
***********************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j, k;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    // display the upper half
    for (i = 1; i <= row; i++) {
        // print spaces
        for (j = 1; j <= row - i; j++) {
            cout << " ";
        }

        for (k = 1; k <= (2 * i - 1); k++) {
            if (k == 1 || k == (2 * i - 1))
                cout << "*";
            else
                cout << " ";
        }
        // new line
        cout << endl;
    }

    // display the lower half
    for (i = 1; i <= row - 1; i++) {
        // print spaces
        for (j = 1; j <= i; j++) {
            cout << " ";
        }

        for (k = 1; k <= (2 * (row - i) - 1); k++) {
            if (k == 1 || k == (2 * (row - i) - 1))
                cout << "*";
            else
                cout << " ";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 5


    *

   * *

  *   *

 *     *

*       *

 *     *

  *   *

   * *

    *




1.3. Java Program & output to print the hollow diamond pattern

Code has been copied
/************************************************
		alphabetacoder.com
Java program to print the hollow diamond pattern
*************************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);
        // declare variables
        int row, i, j, k;

        // take input
        System.out.print("Enter the number of rows: ");
        row = sc.nextInt();

        // new line
        System.out.println("");

        // display the pattern
        // display the upper half
        for (i = 1; i <= row; i++) {
            // print spaces
            for (j = 1; j <= row - i; j++) {
                System.out.print(" ");
            }

            for (k = 1; k <= (2 * i - 1); k++) {
                if (k == 1 || k == (2 * i - 1))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            // new line
            System.out.print("\n");
        }

        // display the lower half
        for (i = 1; i <= row - 1; i++) {
            // print spaces
            for (j = 1; j <= i; j++) {
                System.out.print(" ");
            }

            for (k = 1; k <= (2 * (row - i) - 1); k++) {
                if (k == 1 || k == (2 * (row - i) - 1))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            // new line
            System.out.print("\n");
        }
    }
}

Output


Enter the number of rows: 4


   *

  * *

 *   *

*     *

 *   *

  * *

   *




1.4. Python Program & output to print the hollow diamond pattern

Code has been copied
#***************************************************
#		alphabetacoder.com
#Python program to print the hollow diamond pattern
#***************************************************

# take input
row = int(input("Enter the number of rows: "))

# new line
print("")

# display the pattern
# display the upper half
for i in range(1, row + 1):
    # print spaces
    for j in range(1, row - i + 1):
        print(" ", end = "")

    for k in range(1, 2 * i):
        if k == 1 or k == (2 * i - 1):
            print("*", end = "")
        else:
            print(" ", end = "")
            
    # new line
    print("")

# display the lower half
for i in range(1, row):
    # print spaces
    for j in range(1, i + 1):
        print(" ", end = "")

    for k in range(1, 2 * (row - i)):
        if k == 1 or k == (2 * (row - i) - 1):
            print("*", end = "")
        else:
            print(" ", end = "")
    # new line
    print("")

Output


Enter the number of rows: 6


     *

    * *

   *   *

  *     *

 *       *

*         *

 *       *

  *     *

   *   *

    * *

     *




1.5. C# Program & output to print the hollow diamond pattern

Code has been copied
/**********************************************
  	     alphabetacoder.com
C# program to print the hollow diamond pattern
***********************************************/

using System;

namespace HollowDiamond {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int row, i, j, k;

            // take input
            Console.Write("Enter the number of rows: ");
            row = Convert.ToInt32(Console.ReadLine());

            // new line
            Console.WriteLine("");

            // display the pattern
            // display the upper half
            for (i = 1; i <= row; i++) {
                // print spaces
                for (j = 1; j <= row - i; j++) {
                    Console.Write(" ");
                }

                for (k = 1; k <= (2 * i - 1); k++) {
                    if (k == 1 || k == (2 * i - 1))
                        Console.Write("*");
                    else
                        Console.Write(" ");
                }
                // new line
                Console.Write("\n");
            }

            // display the lower half
            for (i = 1; i <= row - 1; i++) {
                // print spaces
                for (j = 1; j <= i; j++) {
                    Console.Write(" ");
                }

                for (k = 1; k <= (2 * (row - i) - 1); k++) {
                    if (k == 1 || k == (2 * (row - i) - 1))
                        Console.Write("*");
                    else
                        Console.Write(" ");
                }
                // new line
                Console.Write("\n");
            }
            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the number of rows: 3


  *

 * *

*   *

 * *

  *