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

前端 未结 10 1756
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 14:59

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:16

    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");
    }
    

提交回复
热议问题