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

Diamond pattern

Programs to print the diamond pattern have been given here. If the input size of the pattern is s, the number of rows is 2 * s - 1.






1. Program & output to print the diamond pattern




1.1. C Program & output to print the diamond pattern

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

#include <stdio.h>

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

    // take input
    printf("Enter the size: ");
    scanf("%d", & size);

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

    // display the pattern
    // display the upper half
    for (i = 1; i <= size; i++) {
        // print spaces
        for (j = 1; j <= size - i; j++) {
            printf(" ");
        }
        // print *
        for (k = 1; k <= (2 * i - 1); k++) {
            printf("*");
        }
        // new line
        printf("\n");
    }

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

    return 0;
}

Output


Enter the size: 8


       *

      ***

     *****

    *******

   *********

  ***********

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

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

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

  ***********

   *********

    *******

     *****

      ***

       *




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

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

#include <iostream>

using namespace std;

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

    // take input
    cout << "Enter the size: ";
    cin >> size;

    // new line
    cout << endl;

    // display the pattern
    // display the upper half
    for (i = 1; i <= size; i++) {
        // print spaces
        for (j = 1; j <= size - i; j++) {
            cout << " ";
        }
        // print *
        for (k = 1; k <= (2 * i - 1); k++) {
            cout << "*";
        }
        // new line
        cout << endl;
    }

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

    return 0;
}

Output


Enter the size: 10


         *

        ***

       *****

      *******

     *********

    ***********

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

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

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

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

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

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

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

    ***********

     *********

      *******

       *****

        ***

         *




1.3. Java Program & output to print the diamond pattern

Code has been copied
/*****************************************
		alphabetacoder.com
Java program to print the 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 size, i, j, k;

        // take input 
        System.out.print("Enter the size: ");
        size = sc.nextInt();

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

        // display the pattern
        // display the upper half
        for (i = 1; i <= size; i++) {
            // print spaces
            for (j = 1; j <= size - i; j++) {
                System.out.print(" ");
            }
            // print *
            for (k = 1; k <= (2 * i - 1); k++) {
                System.out.print("*");
            }
            // new line
            System.out.println("");
        }

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

Output


Enter the size: 4


   *

  ***

 *****

*******

 *****

  ***

   *




1.4. Python Program & output to print the diamond pattern

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

# take input
size = int(input("Enter the size: "))

# new line
print("")

# display the pattern
# display the upper half
for i in range(1, size + 1):
    # print spaces
    for j in range(1, size - i + 1):
        print(" ", end = "")
    # print *
    for k in range(1, 2 * i):
        print("*", end = "")
    # new line
    print("")

# display the lower half
for i in range(1, size):
    # print spaces
    for j in range(1, i + 1):
        print(" ", end = "")
    # print *
    for k in range(1, 2 * (size - i)):
        print("*", end = "")
    # new line
    print("")

Output


Enter the size: 6


     *

    ***

   *****

  *******

 *********

***********

 *********

  *******

   *****

    ***

     *




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

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

using System;

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

            // take input
            Console.Write("Enter the size: ");
            size = Convert.ToInt32(Console.ReadLine());

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

            // display the pattern
            // display the upper half
            for (i = 1; i <= size; i++) {
                // print spaces
                for (j = 1; j <= size - i; j++) {
                    Console.Write(" ");
                }
                // print *
                for (k = 1; k <= (2 * i - 1); k++) {
                    Console.Write("*");
                }
                // new line
                Console.WriteLine("");
            }

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

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

Output


Enter the size: 5


    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

    *