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

Hollow rhombus pattern

Programs to print the hollow rhombus pattern have been given here.






1. Program & output to print the hollow rhombus pattern




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

Code has been copied
/**********************
  alphabetacoder.com
C program to print the 
hollow rhombus 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
    for (i = row; i >= 1; i--) {
        for (j = 1; j <= i - 1; j++) {
            printf(" ");
        }

        // for the first and last rows
        if (i == 1 || i == row) {
            for (k = 1; k <= row; k++) {
                printf("*");
            }
        }
        // for the other rows
        else {
            for (k = 1; k <= row; k++) {
                // for the first and last columns
                if (k == 1 || k == row) {
                    printf("*");
                } else {
                    printf(" ");
                }
            }
        }
        // new line
        printf("\n");
    }

    return 0;
}

Output


Enter the number of rows: 7


      *******

     *     *

    *     *

   *     *

  *     *

 *     *

*******




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

Code has been copied
/**********************
  alphabetacoder.com
C++ program to print the 
hollow rhombus 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 << "\n";

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

        // for the first and last rows
        if (i == 1 || i == row) {
            for (k = 1; k <= row; k++) {
                cout << "*";
            }
        }
        // for the other rows
        else {
            for (k = 1; k <= row; k++) {
                // for the first and last columns
                if (k == 1 || k == row) {
                    cout << "*";
                } else {
                    cout << " ";
                }
            }
        }
        // new line
        cout << "\n";
    }

    return 0;
}

Output


Enter the number of rows: 4


   ****

  *  *

 *  *

****




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

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

import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        // declare instance of the 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
	for (i = row; i >= 1; i--) {
	    for (j = 1; j <= i - 1; j++) {
		System.out.print(" ");
	    }

	    // for the first and last rows
	    if (i == 1 || i == row) {
		for (k = 1; k <= row; k++) {
		    System.out.print("*");
		}
	    }
	    // for the other rows
	    else {
		for (k = 1; k <= row; k++) {
		    // for the first and last columns
		    if (k == 1 || k == row) {
			System.out.print("*");
		    } else {
			System.out.print(" ");
		    }
		}
	    }
	    // new line
	    System.out.println("");
	}
    }
}

Output


Enter the number of rows: 5


    *****

   *   *

  *   *

 *   *

*****




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

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

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

# display the pattern
for i in range(row, 0, -1):
    for j in range(1, i):
        print(" ", end = "")
    # for the first and last rows
    if i == 1 or i == row:
        for k in range(1, row + 1):
            print("*", end = "")
    # for the other rows
    else:
        for k in range(1, row + 1):
            # for the first and last columns
            if k == 1 or k == row:
                print("*", end = "")
            else:
                print(" ", end = "")
                
    # new line
    print("")

Output


Enter the number of rows: 8


       ********

      *      *

     *      *

    *      *

   *      *

  *      *

 *      *

********




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

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

using System;

namespace HollowRhombus {
    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
            for (i = row; i >= 1; i--) {
                for (j = 1; j <= i - 1; j++) {
                    Console.Write(" ");
                }
        
                // for the first and last rows
                if (i == 1 || i == row) {
                    for (k = 1; k <= row; k++) {
                        Console.Write("*");
                    }
                }
                // for the other rows
                else {
                    for (k = 1; k <= row; k++) {
                        // for the first and last columns
                        if (k == 1 || k == row) {
                            Console.Write("*");
                        } else {
                            Console.Write(" ");
                        }
                    }
                }
                
                // new line
                Console.WriteLine("");
	    }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the number of rows: 6


     ******

    *    *

   *    *

  *    *

 *    *

******