Print hollow square using for loop for user input length.
Steps to build this program
1) Accept length from user
2) First for loop to iterate pointer row wise
3) second inner for loop for to iterate pointer column wise
4) IF statement to compare first and last row for printing star
5) Else condition to print star for first and last column of every row.
Here is the complete code
/* C Program to print Hollow SQUARE */
#include <stdio.h>
int main()
{
int i, j, len;
// Get Length of square from user
printf("Enter length of square: ");
scanf("%d", &len);
printf("\n");
// This for loop to change pointer row wise
for(i=1; i<=len; i++)
{
//This for loop to print *
for(j=1; j<=len; j++)
{
//condtion to print all star for first and last line
//Also to print first and last star of evey line
if (i==1 || i == len || j ==1 || j == len)
{
printf(" *");
}
//this else part to print blank for rows between 1 and n
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Output
Enter length of square: 8
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
Printing spaces can be handled. By providing extra spaces in print statement we can print suitable hollow square
Print Hollow square using While Loop
/* C Program to print Hollow SQUARE */
#include <stdio.h>
int main()
{
int i=1, j=1, len;
// Get Length of square from user
printf("Enter length of square: ");
scanf("%d", &len);
printf("\n");
// This while loop to change pointer row wise
while (i<=len)
{
//This while loop to print star column wise
j=1;
while (j<=len)
{
//condtion to print all star for first and last line
//Also to print first and last star of evey line
if (i==1 || i == len || j == 1 || j == len)
{
printf("*");
}
//this else part to print blank for rows between 1 and n
else
{
printf(" ");
}
j++;
}
printf("\n");
i++;
}
return 0;
}
Output
Enter length of square: 5
*****
* *
* *
* *
*****
Program to print rectangle by accepting width and breadth.
Print hollow square using while loop for user input width and breadth.
Steps to build this program
1) Accept width and breadth from user
2) First while loop to iterate pointer breadth (row) wise
3) Second inner while loop for to iterate pointer width (column) wise
4) IF statement to compare first and last row for printing star
5) Else condition to print star for first and last column of every row.
Here is the complete code
/* C Program to print rectangle based on user input */
#include <stdio.h>
int main()
{
int i=1, j=1;
int breadth,width;
// Get Length of square from user
printf("Enter width square: ");
scanf("%d", &width);
printf("Enter breadth square: ");
scanf("%d", &breadth);
printf("\n");
// This while loop to change pointer row wise
while (i<=breadth)
{
//This while loop to print star column wise
j=1;
while (j<=width)
{
//condition to print all star for first and last line
//Also to print first and last star of evey line
if (i==1 || i == breadth || j == 1 || j == width)
{
printf("*");
}
//this else part to print blank for rows between 1 and n
else
{
printf(" ");
}
j++;
}
printf("\n");
i++;
}
return 0;
}
Output
Enter width square: 5
Enter breadth square: 3
*****
* *
*****
In case if we want to change spacing between rows and column then we need to add more spaces in print statement available in while statement.
Program to print solid rectangle or square based on user input.
- Accept width and height from user
- Write first while loop to print star row wise
- Write second inner while loop to print star column wise
/* C Program to print solid rectangle */
#include <stdio.h>
int main()
{
int i=1, j=1;
int breadth,width;
printf("Enter width square: ");
scanf("%d", &width);
printf("Enter breadth square: ");
scanf("%d", &breadth);
printf("\n");
// This while loop to change pointer row wise
while (i<=breadth)
{
//This while loop to print star column wise
j=1;
while (j<=width)
{
printf ("*");
j++;
}
printf("\n");
i++;
}
return 0;
}
OUTPUT
Enter width square: 6
Enter breadth square: 4
******
******
******
******