C language outputs a diamond pattern on the screen

Source: Internet
Author: User

C language outputs a diamond pattern on the screen
Output case on the screen: **************************************** **************************************** * ***** observe the variation pattern of each row, find the algorithm. The Code is as follows:

# Include <stdio. h> int main () {int I, j, k; for (I = 0; I <7; I ++)/* I is a row, first output the first 7 rows */{for (j = 0; j <6-i; j ++)/* output * leading space */{printf ("");} for (k = 6-i; k <I + 7; k ++)/* output * (observe the variation of each line) */{printf ("*");} printf ("\ n") ;}for (I = 7; I <13; I ++)/* output the last 6 rows */{for (j = 0; j <i-6; j ++) {printf ("") ;}for (k = i-7; k <18-i; k ++) {printf ("*");} printf ("\ n");} return 0 ;}

 

This method cannot dynamically output a diamond image, but only the image. The code after improvement is as follows:
#include<stdio.h>int main(){    int line,i,j,k;    scanf("%d",&line);    for(i=0;i<line;i++)    {        for(j=0;j<line-i-1;j++)        {            printf(" ");        }        for(k=0;k<2*i+1;k++)        {            printf("*");                     }        printf("\n");          }    for(i=line;i<2*line;i++)    {        for(j=0;j<i+1-line;j++)        {            printf(" ");        }        for(k=0;k<2*line-2*(i-(line-1))-1;k++)        {            printf("*");                     }        printf("\n");          }             return 0;}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.