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

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

    Output a diamond in C by asterisk sign

    #include
    main()
    {
    int n, i, j;
    
    printf("Enter a number: ");
    scanf("%d",&n);
    
    /* Create up arrow. */
    for(i=1; i<=n; i++)
    {   /* Left side. */
        for(j=i; j<=n+1; j++)
        {
            printf(" ");
    
        }
        for(j=1; j<=1; j++)
        {
            printf("*");
        }
        /* Middle. */
        for(j=1; j

提交回复
热议问题