Write algorithms step by step ("counting stars ")

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

All the friends who have learned programming know that, in order to learn various syntax structures in programming languages, we should try to solve various strange questions. Among them, "counting stars" seems to be one of them. What is "Counting Stars? Is to print various shapes of "*", positive triangle, inverted triangle, diamond and so on. This blog is purely designed to commemorate our past years.

A) Positive triangle

void star_1(){int outer;int inner;for(outer = 1; outer <10; outer ++){for(inner = 1; inner <= outer; inner ++){printf("%c ", '*');}printf("\n");}}

B) inverted triangle

void star_2(){int outer;int inner;for(outer = 9; outer >= 1; outer --){for(inner = 1; inner <= outer; inner ++){printf("%c ", '*');}printf("\n");}}

C) Left triangle

void star_3(){int outer;int inner;for(outer = -4; outer <= 4; outer ++){for(inner = 1; inner <= (5 - abs(outer)); inner ++){printf("*");}for(; inner <= 5; inner ++){printf(" ");}printf("\n");}}

D) Right Triangle

void star_4(){int outer;int inner;for(outer = -4; outer <= 4; outer ++){for(inner = 1; inner <= abs(outer); inner ++){printf(" ");}for(; inner <= 5; inner ++){printf("*");}printf("\n");}}

E) Vertical Diamond

void star_5(){int outer;int inner;for(outer = -4; outer <= 4; outer ++){for(inner = -4; inner <= abs(outer) -5; inner ++){printf(" ");}for(; inner <= 5 - abs(outer); inner ++){printf("*");}printf("\n");}}

F) Horizontal Diamond

void star_6(){int outer;int inner;for(outer = -9; outer <= 0; outer ++){for(inner = outer; inner < 0; inner ++){printf(" ");}for(; inner <= 9; inner ++){printf("*");}printf("\n");}}

G) Yang Hui triangle

void star_7(){int outer;int inner;for(outer = 1; outer <10; outer ++){for(inner = 1; inner <= outer; inner ++){printf("%3d ", outer * inner);}printf("\n");}}

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.