c language print diamond

angryTom
Release: 2019-10-26 11:26:37
Original
14600 people have browsed it

c language print diamond

c language prints diamond

C language is a process-oriented computer programming language. When we first learn, we They all run C programs through the command line. Let’s take a look at how to write a C program to output a diamond shape on the command line.

Recommended course: C language tutorial

The source code is:

#include
void main()
{
    int n = 6;
    int i, a, b;
    //前4行.上半部分
    for(i = 1; i <= n; i++)//控制行数
    {
    for(  a = n - 1; a >= i; a--)//打印空格
    {
        printf(" ");
    }
    for(  b = 1;b <= 2*i-1; b++)//打印*
    {
        printf("*");
    }
    printf("\n");
    }
    //后3行,下半部分
    for( i = n-1;i >= 1;i--)
    {
        for( a = i;a <= n-1; a++)
        {
            printf(" ");
        }
        for( b = 1;b <= 2*i-1; b++)
        {
            printf("*");
        }
        printf("\n");
    }
}
Copy after login

What is worth noting in this program

This program The source code mainly uses loop nesting of for statements. The general form of a for statement is:

for(循环变量赋初值;循环条件;循环变量增值) {语句}
Copy after login

When the first or second condition is just a semicolon, it means: This condition is always true and always established!

c language format

Program files include: source program file (suffix .c), target file (suffix .obj), executable file (suffix .exe), when the suffix If the name is wrong, the program cannot be executed. Usually the code we write is the source program file, so when saving it, use .c. The program will automatically generate the target file .obj, and then the execution file .exe. Then execute it to get the result.

The above is the detailed content of c language print diamond. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!