Table of Contents
In this tutorial, we will discuss the Display Hollow square star pattern in C using loops
We can display many types of number, Star, Alphabet patterns using for, while and do-while loop in C language.
In this post, we are going to learn how to display Hollow square star pattern Using for, while and do-while loop in C language
This program allows the user to enter the size of the pattern and then it displays Hollow square star pattern using for loop in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int size,row,coloum; printf("Enter the size for Hollow rectangle\n"); scanf("%d",&size); //get input from the user for num1 for (row=1; row<=size; row++){ printf("*"); } printf("\n"); for (coloum=1; coloum<=size-2; coloum++){ for (row=1; row<=size; row++){ if(row==1||row==size){ printf("*"); }else{ printf(" "); } } printf("\n"); } for(row=1; row<=size; row++){ printf("*"); } printf("\n"); getch(); return 0; }
When the above code is executed it produces the following output
Approach
This program allows the user to enter the size of the pattern and then it displays Hollow square star pattern using while loop in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int size,row,coloum; printf("Enter the size for Hollow rectangle\n"); scanf("%d",&size); //get input from the user for num1 while( row<=size+1){ printf("*"); row++; } printf("\n"); coloum=1; while(coloum<=size-2){ row=1; while( row<=size){ if(row==1||row==size){ printf("*"); }else{ printf(" "); } row++; } printf("\n"); coloum++; } row=1; while(row<=size){ printf("*"); row++; } printf("\n"); getch(); return 0; }
When the above code is executed it produces the following output
Approach
This program allows the user to enter the size of the pattern and then it displays Hollow square star pattern using the do-while loop in C language
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int size,row,coloum; printf("Enter the size for Hollow rectangle\n"); scanf("%d",&size); //get input from the user for num1 do{ printf("*"); row++; }while( row<=size+1); printf("\n"); coloum=1; do{ row=1; do{ if(row==1||row==size){ printf("*"); }else{ printf(" "); } row++; }while( row<=size); printf("\n"); coloum++; }while(coloum<=size-2); row=1; do{ printf("*"); row++; }while(row<=size); printf("\n"); getch(); return 0; }
When the above code is executed it produces the following output
Approach
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.