Print star ('*') diamond in C with nested loops?

前端 未结 10 2020
醉酒成梦
醉酒成梦 2021-01-07 14:41

I want to be able to print a diamond like this when the user enters 5 for the diamond. But also will work for any value that is odd and greater than 0.

10条回答
  •  忘掉有多难
    2021-01-07 15:22

    Hi i got to best solution for this problem in less no of lines

    int b=0;
    for(int a=0;b<=50;a=(b<=25) ? a+2 : a-2 ){
        b+=2;
        for(int b=25-a;b>0;b-=2){
            printf(" ");
        }
        for(int c=0;c<=a;c++){
            printf("*");
        }
        printf("\n");
    }
    

提交回复
热议问题