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

前端 未结 10 1644
没有蜡笔的小新
没有蜡笔的小新 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:40

    #include
    main()
    {
    int i,j,k,n;
    scanf("%d",&n);
    n=(n+1)/2;
        for(i=1;i<=n;i++)
        {
            for(j=n;j>=i;j--)
                printf(" ");
            for(k=1;k<=(2*i-1);k++)
                printf("*");
            printf("\n");
        }
        for(i=1;i<=(n-1);i++)
        {
            for(j=0;j<=i;j++)
                printf(" ");
            for(k=(2*n-3);k>=(2*i-1);k--)
                printf("*");
            printf("\n");
    }
    }
    

提交回复
热议问题