C language implementation with asterisks printed on the screen Diamond __c language

Source: Internet
Author: User

A lot of people first feel must be: very simple ah, not just write a few printf statements

Like this:

#define _crt_secure_no_warnings 1

#include <stdio.h>

int main ()
{
    printf ("   *\n");
    printf ("  ***\n");
    printf ("*****\n");
    printf ("*******\n");
    printf ("*****\n");
    printf ("  ***\n");
    printf ("   *\n");
    System ("pause");
    return 0;
}

It seems to be complete, but if I let you print this diamond is very large, there are many rows, many columns (20 rows, 20 columns), you need a lot of asterisks to do, you have to write this. It's too much trouble.

In fact, as long as found the law ——-when should be printed space, when should print an asterisk, packaged into a function, you can write a write only once, no matter how many rows and columns can be used to use the program, the following is a program such as:

#define _crt_secure_no_warnings 1     

#include <stdio.h>        

void pr_start (int _num)
{
    int n=0;
    int i = 0;
    for (n = 1; n <= 2*_num; n++)         
    {
        int temp = n;
        if (n > _num)
        {
            Temp-= 2 * (n-_num);
        }
        for (i = 1; i < 2 * _num; i++)
        {
            if (i>= (_num-temp + 1)) && (i<= (temp + _num-1))  //Find Value
            {
                printf ("*") is required to output * time I;
            }
            else
            {
                printf ("");
            }
        }
        printf ("\ n")
    ;
}

int main ()
{
    int num = 0;
    printf ("Please enter a number:");
    scanf ("%d", &num);
    Pr_start (num);
    System ("pause");
    return 0;
}

7 Rows, 7 columns:

59 rows, 59 columns:

Program only write once, want to output a n row n column of diamond, only need to enter a (n+1)/2 of the number can be.
The key to the program is to determine when the output ' * ', when the output ' (space).

    for (n = 1; n <= 2*_num; n++)         
    {
        int temp = n;
        if (n > _num)
        {
            Temp-= 2 * (n-_num);
        }
        for (i = 1; i < 2 * _num; i++)
        {
            if (i>= (_num-temp + 1)) && (i<= (temp + _num-1))  //Find needed Output * Time I's value
            {
                printf ("*");
            }
            else
            {
                printf ("");
            }
        }
    }

The above code is the essence of this program to achieve the function, input ' * ' and input ' of the law is also in this area. Here do not give a summary, please draw their own conclusions.
Of course, more than one law, you can think more, to think about what methods can be achieved.

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.