100 typical C language programming examples-C and Python (06-10)

Source: Internet
Author: User

[06] format the output
Question: Use the letter "*" to output the C "pattern.
Idea: Use the '*' character to write the letter C on the paper, and then output it by branch. If the output image is large and regular, you can use loops.

C language code int main () {const char * p = "***** \ n" "***** \ n" "** \ n" "** \ n" ** \ n" "***** \ n" "***** \ n "; printf ("% s", p); return 0 ;}
Python code shape = ''' ************************** ''' print (shape)
Comments: There are three forms of strings in Python: single quotes, double quotes, and three quotes. The three expressions are the same and both represent strings. The text format, such as line breaks, can be retained for three quotation marks.

[07]
Question: Output A special pattern. Run it in the c environment. Have a look, Very Beautiful!
A single character is represented in 8 bits, expressed in 256. The standard ASCII uses only half of the seven bits, representing 128 in total. The remaining 128 notation is usually used to extend the representation of ASCII codes, called extended ASCII. For more information about the standard ASCII reference table and common extended ASCII code reference table, see http://www.cplusplus.com/doc/ascii.
[08] multiplication tips
Question: 9*9 tips
Idea: For branch and column considerations, there are 9 rows and 9 columns, I control row, and j control column.
C language code void fun () {for (int I = 0; I <= 9; ++ I) {for (int j = 1; j <= I; ++ j) {printf ("% d * % d = % 2d", I, j, I * j);} printf ("\ n ");}}
Python code def fun (): for I in range (1, 10): for j in range (1, I + 1 ): print ('{0} * {1} = {2 }'. format (j, I, I * j), end = '') print ('')
Output:
1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9
1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16
1*5 = 5 2*5 = 10 3*5 = 15 4*5 = 20 5*5 = 25
1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 5*6 = 30 6*6 = 36
1*7 = 7 2*7 = 14 3*7 = 21 4*7 = 28 5*7 = 35 6*7 = 42 7*7 = 49
1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 5*8 = 40 6*8 = 48 7*8 = 56 8*8 = 64
1*9 = 9 2*9 = 18 3*9 = 27 4*9 = 36 5*9 = 45 6*9 = 54 7*9 = 63 8*9 = 72 9*9 = 81
Comment: use range () to generate a digital sequence, and use the flexible formatting output provided by str. format.

[09] chess board
Question: It is required to output the chess board.
Program Analysis: Use I control rows and j to control columns, and control the output black square or white square according to the sum of I + j.
C language code void fun () {for (int I = 0; I <8; ++ I) {for (int j = 0; j <8; ++ j) {if (I + j) % 2 = 0) printf ("% c", '#'); else printf ("");} printf ("\ n ");}}
Python code def fun (): for I in range (8): for j in range (8): if (I + j) % 2 = 0: print ('#', end = '') else: print ('', end = '') print ()

[10] print the stairs
Question: print the stairs
Program Analysis: each line outputs a certain number of spaces, and then outputs "|_" at the end _"
C language code void fun (int n) {for (int I = 0; I <n; ++ I) {for (int j = 0; j <= 2 * I; ++ j) {printf ("") ;}printf ("|_\ n ");}}
Python code def fun (n): for I in range (n): for j in range (2 * I): print ("", end = '') print ("| _")

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.