Skip to content

Instantly share code, notes, and snippets.

@iamben
Created March 22, 2011 09:20
Show Gist options
  • Save iamben/880970 to your computer and use it in GitHub Desktop.
Save iamben/880970 to your computer and use it in GitHub Desktop.
square.c
#include <stdio.h>
int main(int argc, const char *argv[])
{
int width;
int i = 0, j = 0;
/* input section*/
printf("Input width:");
scanf("%d", &width);
/* outer loop(y-axis) */
while(i < width) {
/* inner loop(x-axis) */
while(j < width) {
if(i != 0 && i != width -1) {
/* if y-axis is not on edge,
* check the y-axis
* */
if(j != 0 && j != width - 1) {
/*
* if x-axis is not on edge, either
* fill with blank.
* */
printf(" ");
} else {
/* x-axis is on edge,
* fill with '*'
* */
printf("*");
}
} else {
/*
* y-axis is on edge,
* fill with '*'
* */
printf("*");
}
/* increment x-axis */
++j;
}
/* next line, print '\n' and reset x-axis */
printf("\n");
j = 0;
/* increment y-axis */
++i;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment