Skip to content

Instantly share code, notes, and snippets.

@HelixSpiral
Created January 6, 2012 19:40
Show Gist options
  • Save HelixSpiral/1572055 to your computer and use it in GitHub Desktop.
Save HelixSpiral/1572055 to your computer and use it in GitHub Desktop.
Creates a command-line diamond
/* Creates a diamond */
/* Needed for standard IO features */
#include <stdio.h>
int main()
{
/* The number of lines we want the diamond to be */
int lines, x, y;
int stars = 1;
/* Figure out what we want for a number */
printf("How many lines do you wish the diamond to be?: ");
scanf("%d", &lines);
printf("Creating a diamond with %d lines.\r\n", lines);
/* Build the top half of the diamond */
for(x = lines/2; x > 0; --x)
{
/* Print spaces */
for(y = 0; y <= x; ++y)
printf(" ");
/* Print stars */
for(y = 1; y <= stars; ++y)
printf("*");
/* New line */
printf("\r\n");
/* Increase stars */
stars += 2;
}
/* Build the bottom half of the diamond */
for(x = 0; x <= lines/2; ++x)
{
for(y = 0; y <= x; ++y)
printf(" ");
for(y = 1; y <= stars; ++y)
printf("*");
printf("\r\n");
stars -= 2;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment