ssoni

diamond.c

Dec 9th, 2021 (edited)
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void)
  6. {
  7.     int size=0;
  8.  
  9.     while (size%2==0)
  10.     {
  11.         size = get_int("What odd size diamond? ");
  12.     }
  13.  
  14.     int spaces=0;
  15.  
  16.     //draw top half of diamond
  17.     for (int i=1; i<=size; i=i+2)
  18.     {
  19.         //print spaces first
  20.         spaces = (size-i)/2;
  21.         for (int j=0; j<spaces; j++)
  22.         {
  23.             printf(" ");
  24.         }
  25.         //print the hashes next
  26.         for (int j=0; j<i; j++)
  27.         {
  28.             printf("#");
  29.         }
  30.         printf("\n");
  31.     }
  32.  
  33.     //draw bottom half
  34.     for (int i=size-2; i>=1; i=i-2)
  35.     {
  36.          //print spaces first
  37.         spaces = (size-i)/2;
  38.         for (int j=0; j<spaces; j++)
  39.         {
  40.             printf(" ");
  41.         }
  42.         //print the hashes next
  43.         for (int j=0; j<i; j++)
  44.         {
  45.             printf("#");
  46.         }
  47.         printf("\n");
  48.  
  49.     }
  50.  
  51. }
Add Comment
Please, Sign In to add comment