Print the Hollow Diamond Number Pattern

      1    

   2 2   

  3   3  

 4     4 

5       5

 4     4 

  3   3  

   2 2   

    1    


Problem Statement:- Program to Print the Hollow Diamond Number Pattern.

Sample Input/Output:-


Sample Input First:9

Sample Output First: 

    1
   2 2
  3   3
 4     4
5       5
 4     4
  3   3
   2 2
    1

Sample Input Second: 7


Sample Output Second: 

   1
  2 2
 3   3
4     4
 3   3
  2 2
   1

Data requirement:-


   Input Data:- row_size

  Output Data:- x

  Additional Data:-out, in or inn(for python), print_control_x

Program in C

Here is the source code of the C Program to Print the Hollow Diamond Number Pattern.

Code:

#include<stdio.h>
int main()
{
    printf("Enter the row size:");
    int row_size;
    scanf("%d",&row_size);
    int in,out;
    int print_control_x=row_size/2+1,x=1;

     for(out=1;out<=row_size;out++)
       {
           for(in=1;in<=row_size;in++)
           {
               if(in==print_control_x || in==row_size-print_control_x+1)
               {
                printf("%d",x);
                 }
               else
               {
                 printf(" ");
               }
             }
            if(out<=row_size/2){
           print_control_x--;
               x++;
               }
           else{
              print_control_x++;
              x--;
           }
            printf("\n");
     }
}

Input/Output:
Enter the row size:9
    1
   2 2
  3   3
 4     4
5       5
 4     4
  3   3
   2 2
    1

Program in C++

Here is the source code of the C++ Program to Print the Hollow Diamond Number Pattern.

Code:

#include<iostream>
using namespace std;
int main()
{
    cout<<"Enter the row size:";
    int row_size;
    cin>>row_size;
    int in,out;
   int print_control_x=row_size/2+1,x=1;

     for(out=1;out<=row_size;out++)
       {
           for(in=1;in<=row_size;in++)
           {
               if(in==print_control_x || in==row_size-print_control_x+1)
               {
               cout<<x;
                 }
               else
               {
                 cout<<" ";
               }
             }
           if(out<=row_size/2){
           print_control_x--;
               x++;
               }
           else{
              print_control_x++;
              x--;
           }
            cout<<"\n";
     }
}

Input/Output:
Enter the row size:7
   1
  2 2
 3   3
4     4
 3   3
  2 2
   1

Program in Java

Here is the source code of the Java Program to Print the Hollow Diamond Number Pattern.

Code:

import java.util.Scanner;
public class NumberPattern19 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
        System.out.println("Enter the row size(Odd Number):");
       int out,in;
       int row_size=cs.nextInt();
       int print_control_x=row_size/2+1,x=1;
       for(out=1;out<=row_size;out++)
       {
           for(in=1;in<=row_size;in++)
           {
               if(in==print_control_x || in==row_size-print_control_x+1)
               {
                  System.out.print(x);
               }
               else
               {
                System.out.printf(" ");    
               }
           }
           if(out<=row_size/2){
           print_control_x--;
               x++;
               }
           else{
              print_control_x++;
              x--;
           }
           System.out.println(); 
       }
       cs.close();
}}

Input/Output:
Enter the row size(Odd Number):
9
    1    
   2 2   
  3   3  
 4     4 
5       5
 4     4 
  3   3  
   2 2   
    1    

Program in Python

Here is the source code of the Python Program to Print the Hollow Diamond Number Pattern.

Code:

row_size=int(input("Enter the row size:"))
print_control_x=row_size//2+1
x=1
for out in range(1,row_size+1):
    for inn in range(1,row_size+1):
        if inn==print_control_x or inn==row_size-print_control_x+1:
            print(x,end="")
        else:
            print(" ", end="")
    if out <= row_size // 2:
        print_control_x-=1
        x+=1
    else:
        print_control_x+=1
        x-=1
    print("\r")

Input/Output:
Enter the row size:5
  1  
 2 2 
3   3
 2 2 
  1  

Post a Comment

0 Comments