Sep 4, 2020

C program to print hollow square number pattern

Ex: Write a C program to print hollow square number pattern. How to write a C program to print hollow square number pattern. C program to print hollow square number pattern. 


Input from user:

Enter the number: 5


Expected output:

22222

2       2

2       2

2       2

22222





  Step by step logic of the given program:


To understand this pattern, first you need to understand previous exercises hollow square star pattern.


1. Accept input number from user declare variable say no.


2. Run outer loop from 1 to no, which shows rows in pattern.


3. Run inner loop from 1 to no to print columns in pattern.


4. Inside the inner loop using if statement give conditions to print number:

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


5. Otherwise print spaces, to make it hollow.


6. Outside the block of inner for loop, print newline \n.



Program:


#include<stdio.h>

void main()

{

int i,j,no;

printf("Enter the number:");

scanf("%d",&no);

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

{

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

{

/*Conditions to print number*/
                if(i==1||i==no||j==1||j==no)

{

    /*you can enter any number(symbol) here*/         
                    printf("2");

}

else

{

    /*To print spaces*/

                 printf(" ");

}

}

/*For next row*/

            printf("\n");

}

}



Above program shows the following output:


C program to print hollow square number pattern


No comments:

Post a Comment

If you have any doubts, please discuss here...