Write a program to print hollow rectangle star pattern

19 July 2022

Write a program to print hollow rectangle star pattern


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get the number of rows as the input and print the hollow rectangle star pattern

Input

N= 4      

Output

*  *  *  * 

*          *

*          *

*  *  *  *

 

C Program

#include <stdio.h>

int main()

 {

    int i, j, N;

    printf("Enter the number of rows: ");

    scanf("%d", &N);

    for(i=1; i<=N; i++)

    {

        for(j=1; j<=N; j++)

        {

            if(i==1 || i==N || j==1 || j==N)

            printf("*");

            else

            printf(" ");

          }

      printf("\n");

      }

    return 0;

 }

 

C++ Program

#include <iostream>

using namespace std;

int main()

 {

    int i, j, N;

    cout<<"Enter the number of rows: ";

    cin>>N;

    for(i=1; i<=N; i++)

    {

        for(j=1; j<=N; j++)

        {

            if(i==1 || i==N || j==1 || j==N)

            cout<<"*";

            else

            cout<<" ";

          }

      cout<<"\n";

      }

    return 0;

 }

 

Java Program

import java.util.Scanner;

public class Main

{

                public static void main(String[] args) {

                                Scanner sc = new Scanner(System.in);

                                System.out.println("Enter the number of rows: ");

                                int n= sc.nextInt();

                                for(int i=1; i<=n; i++)

    {

                for(int j=1; j<=n; j++)

                {   

                     if(i==1 || i==n || j==1 || j==n)

            System.out.print("*");

            else

            System.out.print(" ");

                }

                System.out.println();

     }

    

                }

}

 

Python Program

n=int(input("Enter the number of rows: "))

for i in range(1,n+1):

    for j in range(1,n+1):

        if(i==1 or i==n or j==1 or j==n):

            print("*",end="")

        else:

            print(" ",end="")

    print()


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !