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

Half diamond pattern

Programs to print the half diamond pattern have been given here. The number of rows of the pattern is taken as input.






1. Program & output to print the half diamond pattern




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

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

#include <stdio.h>

int main() {
    // declare variables
    int row, i, 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 *
        for (k = 1; k <= i; k++) {
            printf("*");
        }
        // new line
        printf("\n");
    }

    // display the lower half
    for (i = 1; i <= row - 1; i++) {
        // print *
        for (k = 1; k <= row - i; k++) {
            printf("*");
        }
        // new line
        printf("\n");
    }

    return 0;
}

Output


Enter the number of rows: 5


*

**

***

****

******

****

***

**

*




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

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

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, 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 *
        for (k = 1; k <= i; k++) {
            cout << "*";
        }
        // new line
        cout << endl;
    }

    // display the lower half
    for (i = 1; i <= row - 1; i++) {
        // print *
        for (k = 1; k <= row - i; k++) {
            cout << "*";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 8


*

**

***

****

*****

******

*******

********

*******

******

*****

****

***

**

*




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

Code has been copied
/***********************************************
  	        alphabetacoder.com
Java program to print the half 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, 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 *
            for (k = 1; k <= i; k++) {
                System.out.print("*");
            }
            // new line
            System.out.println("");
        }

        // display the lower half
        for (i = 1; i <= row - 1; i++) {
            // print *
            for (k = 1; k <= row - i; k++) {
                System.out.print("*");
            }
            // new line
            System.out.println("");
        }
    }
}

Output


Enter the number of rows: 4


*

**

***

****

***

**

*




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

Code has been copied
#*************************************************
#  	        alphabetacoder.com
#Python program to print the half 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):
    for j in range(1, i + 1):
        # print *
        print("*", end = "")
    # new line
    print("")
    
# display the lower half
for i in range(1, row):
    for j in range(1, row - i + 1):
        # print *
        print("*", end = "")
    # new line
    print("")

Output


Enter the number of rows: 6


*

**

***

****

*****

******

*****

****

***

**

*




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

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

using System;

namespace HalfDiamond {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int row, i, 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 *
                for (k = 1; k <= i; k++) {
                    Console.Write("*");
                }
                // new line
                Console.WriteLine("");
            }

            // display the lower half
            for (i = 1; i <= row - 1; i++) {
                // print *
                for (k = 1; k <= row - i; k++) {
                    Console.Write("*");
                }
                // new line
                Console.WriteLine("");
            }

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

Output


Enter the number of rows: 7


*

**

***

****

*****

******

*******

******

*****

****

***

**

*