Write a Program to Print Square Using * Character

Posted in

Write a Program to Print Square Using * Character
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    Printing patterns using * is one of the most common interview questions.

    In this article, we will learn how to print a hollow square bracket in a console using C, C++, and Python programming languages. The Syntax for all these three programming languages is different, but the algorithm and logic would remain the same to print the square bracket.

    Algorithm

    • Create a nested loop from 0 to n where n is the length and width of the square.
    • Inside the nested loop print * if the outer loop value is 0 or n-1, or the inner loop value is 0 or n-1.
    • For all the other values, print the empty space " ".
    • With this, the star will be printed only for the outer parameter of the square.

    Print Square Pattern in C

    #include<stdio.h>
    
    int main()
    {
    	int i, j, n;
    	//input the width or height of the square
    	printf("Enter the Height of Square: ");
    	scanf("%d", &n);
    	
    	//create a nested loop
    	//from 0 to n
    	for(i =0; i<n; i++)
    	{
    		for(j=0;j<n;j++)
    		{	
    			//print * if the value of 
    			//outer or inner loop value is 0 or n-1
    			if(i==0||i==n-1||j==0||j==n-1)
    			{
    				printf("*");
    			}
    			else
    			//else print empty spaces
    			{
    				printf(" ");
    			}
    		}
    		printf("\n");
    	}
    	
    	return 0;
    }

    Output

    Enter the Height of Square: 12
    ************
    *          *
    *          *
    *          *
    *          *
    *          *
    *          *
    *          *
    *          *
    *          *
    *          *
    ************

    Print Square Pattern in C++

    #include <iostream>
    using namespace std;
     
    int main()
    {	
    	int i, j, n;
    	
    	//input the width or height of the square
    	cout<<"Enter the Height of Square: "; cin>>n;
    	
    	//create a nested loop
    	//from 0 to n
    	for(i =0; i<n; i++)
    	{
    		for(j=0;j<n;j++)
    		{	
    			//print * if the value of 
    			//outer or inner loop value is 0 or n-1
    			if(i==0||i==n-1||j==0||j==n-1)
    			{
    				cout<<"*";
    			}
    			else
    			//else print empty spaces
    			{
    				cout<<" ";
    			}
    		}
    		cout<<"\n";
    	}
    	
    	return 0;
    }

    Output

    Enter the Height of Square: 13
    *************
    *           *
    *           *
    *           *
    *           *
    *           *
    *           *
    *           *
    *           *
    *           *
    *           *
    *           *
    *************

    Print Square Pattern in Python

    n = int(input("Enter the Height of Square: "))
    
    # create a nested loop
    # from 0 to n
    
    for i in range(n):
        for j in range(n):
            #print * if the value of 
            #outer or inner loop value is 0 or n-1
            if (i==0 or i==n-1 or j==0 or j==n-1):
                print("*", end="")
            else:
                # else print empty spaces
                print(" ", end ="")
        print()

    Output

    Enter the Height of Square: 14
    **************
    *            *
    *            *
    *            *
    *            *
    *            *
    *            *
    *            *
    *            *
    *            *
    *            *
    *            *
    *            *
    **************

    Complexity Analysis

    • Time Complexity: O(N^2), is the time complexity of the above program because we are using a nested loop.
    • Space Complexity: O(1).

    Wrapping Up!

    In this article, we learn how to write a program in C, C++, and Python to print a hollow square box in a console using the * character.  The algorithm for this program is very simple we just need to print the * character for all the points where i or j is either 0 or n-1, and for the rest of the points, we just print blank space.

    People are also reading:

    Leave a Comment on this Post

    0 Comments