Hello, I made a simple program for my C class and just wanted to see what others thought about my code so I can improve. Is there a better way of doing what I was attempting? The program basically asks for an odd number which correlates to the size of the diamond which gets printed out
here's the code, any suggestions are appreciated!
#include <stdio.h>
#include <stdbool.h>
int main(int argc, const char * argv[])
{
int max_stars;
bool odd = false;
do {
printf("How many stars wide do you want your diamond size to be? (odd): ");
scanf("%d", &max_stars);
// checks for odd number
if(max_stars%2 == 1)
odd = true;
}while(odd == false);
int num_spaces = max_stars - 1;
int current_stars = 1;
// top half of diamond
for(int i = num_spaces; i > 0; i-=2) {
for(int i = 0; i < num_spaces/2; i++)
printf(" ");
for(int i = 0; i < current_stars; i++)
printf("*");
printf("\n");
current_stars += 2;
num_spaces -= 2;
}
// bottom half of diamond
for(int i = num_spaces; i <= max_stars; i+=2) {
for(int i = 0; i < num_spaces/2; i++)
printf(" ");
for(int i = 0; i < current_stars; i++)
printf("*");
printf("\n");
current_stars -= 2;
num_spaces += 2;
}
return 0;
}