Program to print numbers with diamond pattern

write a program where each column represents same number according to given example:

Examples :

Input : 5
Output :
  1
 212
32123
 212
  1

Input : 7
Output :
   1
  212
 32123
4321234
 32123
  212
   1

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print diamond pattern 
#include<iostream>
using namespace std;
  
void display(int n) 
    // sp stands for space 
    // st stands for number 
    int sp = n / 2, st = 1; 
  
    // Outer for loop for number of lines 
    for (int i = 1; i <= n; i++) 
    
  
        // Inner for loop for printing space 
        for (int j = 1; j <= sp; j++) 
            cout << " "
  
        // Inner for loop for printing number 
        int count = st / 2 + 1; 
        for (int k = 1; k <= st; k++) 
        
            cout << count; 
            if (k <= st / 2) 
                count--; 
            else
                count++; 
        
  
        // To goto next line 
        cout << endl; 
        if (i <= n / 2)
        
  
            // sp decreased by 1 
            // st increased by 2 
            sp = sp - 1; 
            st = st + 2; 
        
  
        else 
        
  
            // sp increased by 1 
            // st decreased by 2 
            sp = sp + 1; 
            st = st - 2; 
        
    
  
// Driver code 
int main() 
    int n = 7; 
    display(n); 
  
// This code is contributed by Smitha

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print diamond pattern
import java.util.Scanner;
  
class Pattern {
    void display(int n)
    {
        // sp stands for space
        // st stands for number
        int sp = n / 2, st = 1;
  
        // Outer for loop for number of lines
        for (int i = 1; i <= n; i++) {
  
            // Inner for loop for printing space
            for (int j = 1; j <= sp; j++)
                System.out.print(" ");
  
            // Inner for loop for printing number
            int count = st / 2 + 1;
            for (int k = 1; k <= st; k++) {
                System.out.print(count);
                if (k <= st / 2)
                    count--;
                else
                    count++;
            }
  
            // To goto next line
            System.out.println();
            if (i <= n / 2) {
  
                // sp decreased by 1
                // st increased by 2
                sp = sp - 1;
                st = st + 2;
            }
  
            else {
  
                // sp increased by 1
                // st decreased by 2
                sp = sp + 1;
                st = st - 2;
            }
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        Pattern p = new Pattern();
        int n = 7;
        p.display(n);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print diamond pattern
def display(n):
      
    # sp stands for space
    # st stands for number
    sp = n // 2
    st = 1
      
    # Outer for loop for number of lines
    for i in range(1, n + 1):
          
        # Inner for loop for printing space
        for j in range(1, sp + 1):
            print (" ", end = ' ')
  
        # Inner for loop for printing number         
        count = st // 2 + 1
          
        for k in range(1, st + 1):
            print (count, end = ' ')
            if (k <= (st // 2)):
                count -= 1
            else:
                count += 1
                  
        # To go to next line     
        print() 
          
        if (i <= n // 2):
              
            # sp decreased by 1
            # st decreased by 2 
            sp -= 1
            st += 2
        else:
              
            # sp increased by 1 
            # st decreased by 2
            sp += 1
            st -= 2 
              
# Driver code        
n = 7
display(n)
  
# This code is contributed by DrRoot_

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print diamond pattern
using System;
  
class Pattern {
    void display(int n)
    {
        // sp stands for space
        // st stands for number
        int sp = n / 2, st = 1;
  
        // Outer for loop for number of lines
        for (int i = 1; i <= n; i++) {
  
            // Inner for loop for printing space
            for (int j = 1; j <= sp; j++)
                Console.Write(" ");
  
            // Inner for loop for printing number
            int count = st / 2 + 1;
            for (int k = 1; k <= st; k++) {
                Console.Write(count);
                if (k <= st / 2)
                    count--;
                else
                    count++;
            }
  
            // To goto next line
            Console.WriteLine();
            if (i <= n / 2) {
  
                // sp decreased by 1
                // st increased by 2
                sp = sp - 1;
                st = st + 2;
            }
  
            else {
  
                // sp increased by 1
                // st decreased by 2
                sp = sp + 1;
                st = st - 2;
            }
        }
    }
  
    // Driver code
    public static void Main()
    {
        Pattern p = new Pattern();
        int n = 7;
        p.display(n);
    }
}
//This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to print diamond pattern
  
function display($n)
    {
        // sp stands for space
        // st stands for number
        $sp = $n / 2;
        $st = 1;
  
        // Outer for loop for
        // number of lines
        for ($i = 1; $i <= $n; $i++) 
        {
  
            // Inner for loop for 
            // printing space
            for ($j = 1; $j <= $sp; $j++)
                echo " ";
  
            // Inner for loop for 
            // printing number
            $count = $st / 2 + 1;
            for ($k = 1; $k <= $st; $k++) 
            {
                // To hide floating 
                // value using floor()
                echo floor($count); 
                if ($k <= $st / 2)
                    $count--;
                else
                    $count++;
            }
  
            // for next line
            echo "\n";
            if ($i <= $n / 2) 
            {
  
                // sp decreased by 1
                // st increased by 2
                $sp = $sp - 1;
                $st = $st + 2;
            }
  
            else 
            {
  
                // sp increased by 1
                // st decreased by 2
                $sp = $sp + 1;
                $st = $st - 2;
            }
        }
    }
  
    // Driver code
       $n = 7;
       display($n);
  
  
// This article is contributed by mits 
  
?>

chevron_right



Output :

   1
  212
 32123
4321234
 32123
  212
   1


My Personal Notes arrow_drop_up

Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator. I just love Programming languages and love to know new concepts everyday,every minute,every second. Here to help Other GEEKS!!!

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.