C - Hình Thoi

C - Hình Thoi

 

Question:

image

C++ Coding, creating a diamond bsed on user input






Answer:


/*this can be called as hallow diamond shape*/

 

#include<stdio.h>

int main() {
    int i, space, rows, star=0;
    printf("enter length of the side:");
     scanf("%d",&rows);
    /* Printing  the upper triangle */
    for(i = 1; i <= rows; i++) {
        /* spaces */
        for(space = 1; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        while(star != (2*i - 1)) {
         if(star == 0)
                printf("*");
            else if(star==2*i-2)
                 printf("*");
            else
                printf(" ");
            star++;
        }
        star=0;
        /* moving  to next row */
        printf("\n");
    }
    rows--;
    /* Printing the lower triangle */
    for(i = rows;i >= 1; i--) {
        /* Printing the  spaces */
        for(space = 0; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        star = 0;
        while(star != (2*i - 1)) {
         if(star == 0)
          printf("*");
            else if (star==2*i-2)
                printf("*");
            else
                printf(" ");
            star++;
        }
        printf("\n");
    }

    return 0;
}








image
Previous Post
Next Post

post written by:

Related Posts

0 Comments: