logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jun03/square.c

/*
Prompts the user for a size n ,then outputs an n*n square of stars
using a nested for loop
*/
#include <stdio.h>
int main()
{
int i, j, size; //variable declarations for the loops and size
printf("Size of Square: "); //prompting user for the size
scanf("%d", &size); //getting the user input and assigning it to size
/*
C allows the use of nested loops. In this program, we will be using nested loops to print
a size x size square of stars.
*/
for(i = 1; i <= size; i++){
/*
this outer loop (the i loop) will handle the printing of the rows,
it will run a total of size times, so if size were 5, this
loop will run 5 times.
*/
for(j = 1; j <= size; j++){
/*
We need an inner for loop (using j) to handle printing the columns
For every *one* time the outer for loop runs, this inner for loop will run
size number of times. So if size is 5, this loop will run 5 times for every
one time the outer loop runs, for a total of 25 times overall.
*/
printf("*");
}
printf("\n"); //newline so that after the inner loop gets done, we can go on to the next row.
}
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX