C Program: Empty Diamond (or Triangle)

Here is another C program for displaying an empty diamond. This is another C program code for displaying a triangle. The user will input the base for the diamond (or triangle). The maximum input for this program is 40 although can easily be changed when implemented.

C program for an empty diamond

#include 
#include 

int base, row, column, temp;

main()
{
    // Input Area - do-while is used to validate such that only inputs from 0 to 40 are accepted
    do
    {
         system("cls");
         printf("Enter base : ");
         scanf("%d", &base);
    } while ((base<0) || (base>40));

    //Output Area
    temp=(2*base)-1;
    for (row=1;row<=temp;row++)
    {
        for (column=1;column<=temp;column++)
        {
          if ((row<=base) && ((column==((base-1)+row)) || (column==((base+1)-row)))) //upper half of the triangle
                 printf("*",column);
          else if ((row>base) && ((column==(((3*base)-1)-row)) || (column==((row+1)-base)))) //lower half of the triangle
                 printf("*",column);
          else
              printf(" ");
        }
        printf("\n");
     }
    system("pause");
}
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • BlogMemes
  • Fark
  • StumbleUpon
  • NewsVine
  • Spurl
  • Technorati
  • TwitThis

Post a Comment