-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex7.c
More file actions
36 lines (29 loc) · 1.03 KB
/
ex7.c
File metadata and controls
36 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Write a program that reads in the side of a square and then prints that square out of asterisks. Your program should work for squares of all side sizes between 1 and 20. For example, if your program reads a size of 4, it should print
// ****
// ****
// ****
// ****
#include <stdio.h>
int main()
{
int side, i, j;
// Prompt the user for the side length of the square
printf("Enter the side length of the square (1-20): ");
scanf("%d", &side);
// Validate the input to ensure it's between 1 and 20
if (side < 1 || side > 20)
{
printf("Side length must be between 1 and 20.\n");
return 1; // Exit the program with an error code
}
// Use nested loops to print the square of asterisks
for (i = 0; i < side; i++)
{ // Outer loop for each row
for (j = 0; j < side; j++)
{ // Inner loop for each column in the row
printf("*");
}
printf("\n"); // Move to the next line after printing each row
}
return 0; // Indicate successful completion
}