16.Write a program in c to print the pattern of a hollow square using a while loop.
Write a C program to print a series of hollow squares or rectangles (*) using a for loop. How to print an hollow square star or rectangle pattern of N lines using programming.
for loop logic to print an empty square star or rectangle pattern in a C program.
Logic
input
Enter the number of lines: 5
exit
*****
* *
* *
* *
*****
Logic to print hollow square star pattern
The pattern above is similar to the square star pattern of N rows and N columns. Here the star is printed only for the first and last column or for the first and last row.
Step-by-step descriptive logic for printing a blank square star pattern.
1. Enter the number of lines to be printed from the user. Store it in a variable, for example N.
2. Run an outer loop from 1 to N to iterate through the rows. Define the for(i=1; i<=N; i++) structure for this loop.
3. Run an inner loop from 1 to N to iterate over the columns. Define a loop with the structure for(j=1; j<=N; j++).
4. Inside the inner loop, type an asterisk for the first and last row or the first and last column. Which, if i==1, i==N, j==1, or j==N, will output
an asterisk, otherwise a space.
5. After printing all the columns of a row, go to the next row, i.e. print an empty row after the inner loop.
Program to print hollow square pattern
/**
* Program in c to show hollow square star pattern
*/
#include <stdio.h>
int main()
{
int i, j, N;
/* accept number of rows as input from user */
printf("\nEnter number of rows:\n ");
scanf("%d", &N);
/* Iterate through each row */
for(i=1; i<=N; i++)
{
/* Iterate through each column */
for(j=1; j<=N; j++)
{
if(i==1 || i==N || j==1 || j==N)
{
/* print the star for 1st Nth column and row */
printf("*");
}
else
{
printf(" ");
}
}
/* Move to the next line/row */
printf("\n");
}
return 0;
}
Previous :-->> 15.Write a program in c language to find the sum of all digits of a given number.
-->> NEXT:
17. Write a program in c to check if a given number is a perfect number or not using a do while loop.
-->>ALL
Loops Assignments in c