Print the Hollow Half Pyramid Number Pattern

1    

22   

3 3  

4   4 

55555



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

Sample Input/Output:-


Sample Input First:5

Sample Output First: 

1
22
3 3
4   4
55555

Sample Input Second: 6


Sample Output Second: 

1
22
3  3
4   4
5    5
666666

Data requirement:-


   Input Data:- row_size

  Output Data:-out

  Additional Data:-in or inn(for python)

Program in C

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

Code:

#include<stdio.h>
int main()
{
    int out,in;
    printf("Enter the row size:");
    int row_size;
    scanf("%d",&row_size);
        for(out=1;out<=row_size;out++)
       {
           for(in=1;in<=row_size;in++)
           {
               if(in==1 || out==in || out==row_size)
               {
    printf("%d",out);
     }
     else
         {
             printf(" ");
         }}
        printf("\n");
}}

Input/Output:
Enter the row size:5
1
22
3 3
4   4
55555

Program in C++

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

Code:

#include<iostream>
using namespace std;
int main()
{
    cout<<"Enter the row size:";
    int row_size;
    cin>>row_size;
    int in,out,p;
      for(out=1;out<=row_size;out++)
       {
           for(in=1;in<=row_size;in++)
           {
               if(in==1 || out==in || out==row_size)
               {
               cout<<out;
                 }
               else
               {
                 cout<<" ";
               }
             }
            cout<<"\n";
     }
}

Input/Output:
Enter the row size:
6
1
22
3  3
4   4
5    5
666666

Program in Java

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

Code:

import java.util.Scanner;
public class NumberPattern23 {

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

Input/Output:
Enter the row size:
4
1   
22  
3  3 
4444


Program in Python

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

Code:

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

Input/Output:
Enter the row size:5
1    
22   
3 3  
4   4 
55555

Post a Comment

0 Comments