Exercise the cyclic statements in C language, and execute the cyclic statements in C Language

Source: Internet
Author: User

Exercise the cyclic statements in C language, and execute the cyclic statements in C Language

Note: All exercise questions are from the introduction to C Language

I. do statements

1. Calculate the sum and average of multiple integers.

# Include <stdio. h> int main (void) {int sum = 0; // and int cnt = 0; // integer number int retry; // judge whether to continue do {int t; printf ("enter an integer:"); scanf ("% d", & t); sum = sum + t; cnt = cnt + 1; printf ("continue? <Yes... 0/No... 9: "); scanf (" % d ", & retry) ;}while (retry = 0); printf (" and % d, average %. 2f. \ N ", sum, (double) sum/cnt); return 0 ;}

 

Ii. whie statements

1. Each integer from 0 to the input positive integer is displayed progressively

# Include <stdio. h> int main (void) {int I, n; printf ("enter a positive integer:"); scanf ("% d", & n); I = 0; while (I <= n) printf ("% d", I ++); // The value of I increases progressively after display printf ("\ n"); return 0 ;}

 

2. Write a program to display all positive and even numbers smaller than the input values in ascending order.

# Include <stdio. h> int main (void) {int I, n; printf ("enter a positive integer:"); scanf ("% d", & n); I = 0; while (I <= n) {I = I + 2; if (I <n) // you must first judge the incremental value, check whether printf ("% d", I) ;}printf ("\ n"); return 0 ;}

 

3. Write a program to show + and-alternately. The total number is equal to the entered integer.

# Include <stdio. h> int main (void) {int num; int I = 0; printf ("positive integer:"); scanf ("% d", & num ); while (I <num) {if (++ I % 2) putchar ('+'); else putchar ('-');} return 0 ;}

 

4. Reverse display of Positive Integers

# Include <stdio. h> int main (void) {int n; do {printf ("enter a positive integer:"); scanf ("% d", & n ); if (n <= 0) puts ("do not enter a non-positive integer. ") ;}While (n <= 0); printf (" the result of reverse display of this integer is "); while (n> 0) {printf (" % d ", n % 10); n/= 10;} puts (". "); Return 0 ;}

 

III. for statements

1. Write a program to display 1234567890 cyclically based on the input integer. The number of digits displayed is the same as the input integer.

# Include <stdio. h> int main (void) {int I, j, n; j = 1; printf ("enter an integer:"); scanf ("% d ", & n); for (I = 0; I <n; I ++) {printf ("% d", j); j ++; if (j> 9) j-= 10;} printf ("\ n"); return 0 ;}

 

2. Compile a program to display the table of high birth and standard weight. Note: The displayed height range and interval are controlled by the entered integer. The standard weight is accurate to the second digit after the decimal point.

# Include <stdio. h> int main () {int beginHigh = 0, endHigh = 0; float weight = 0; int margin = 0; printf ("start value (cm ):"); scanf ("% d", & beginHigh); printf ("end value (cm):"); scanf ("% d", & endHigh ); printf ("interval value (cm):"); scanf ("% d", & margin); for (int I = beginHigh; I <= endHigh; I + = margin) {weight = (I-100) * 0.9; // This is a formula for calculating the standard weight printf ("% d cm \ t %. 2f \ n ", I, weight); // \ t indicates a Tab, equivalent to pressing a Tab key} return 0 ;}

 

3. display all the approximate values of the input integer.

# Include <stdio. h> int main (void) {int I, n; printf ("INTEGER:"); scanf ("% d", & n); for (I = 1; I <= n; I ++) if (n % I = 0) printf ("% d", I); putchar ('\ n'); return 0 ;}

 

4. Write a program and enter an integer to display the integer '*'. Line feed is performed for each display of 5.

# Include <stdio. h> int main () {printf ("enter an integer:"); int a; scanf ("% d", & a); int B = 0; for (int I = 0; I <a; I ++) {if (B = 5) {printf ("\ n"); B = 0 ;} printf ("*"); B = B + 1 ;}}

 

 

Iv. Multiple cycles

1. display the 9-9 multiplication table

#include<stdio.h>int main(void){    int i,j;    for(i = 1;i <= 9;i++){        for(j = 1;j <= 9;j++)            printf("%3d",i * j);        putchar('\n');    }        return 0; } 

 

2. Write a program to add a horizontal and vertical title to the 9-9 multiplication table.

# Include <stdio. h> int main () {int I, j; // print the first line printf ("|"); for (I = 1; I <= 9; I ++) {printf ("% d", I) ;}printf ("\ n"); // print the second line for (I = 1; I <= 32; I ++) printf ("-"); printf ("\ n"); // start printing the 99 multiplication table. Each row must start with a row number for (I = 1; I <= 9; I ++) // row {printf ("% d |", I); for (j = 1; j <= 9; j ++) // column {printf ("% 2d", I * j);} printf ("\ n");} return 0 ;}

 

3. Draw a rectangle

# Include <stdio. h> int main (void) {int I, j; int height, width; puts ("let's draw a rectangle. "); Printf (" high: "); scanf (" % d ", & height); printf (" width: "); scanf (" % d ", & width); for (I = 1; I <= height; I ++) {// The rectangle has a height row for (j = 1; j <= width; j ++) // display width putchar ('*') in each row; putchar ('\ n'); // line feed} return 0 ;}

 

4, (1) display the right angleBottom leftRight triangle of the same waist

# Include <stdio. h> int main (void) {int I, j, len; puts ("right angle at the bottom left of the isosceles right triangle. "); Printf (" side length: "); scanf (" % d ", & len); for (I = 1; I <= len; I ++) {for (j = 1; j <= I; j ++) putchar ('*'); putchar ('\ n');} return 0 ;}

 

 

(2) display the right angleBottom rightRight triangle of the same waist

# Include <stdio. h> int main (void) {int I, j, len; puts ("right angle at the bottom left of the isosceles right triangle. "); Printf (" side length: "); scanf (" % d ", & len); for (I = 1; I <= len; I ++) {for (j = 1; j <= len-I; j ++) putchar (''); for (j = 1; j <= I; j ++) putchar ('*'); putchar ('\ n');} return 0 ;}

 

 

(3) display the right angleUpper leftRight triangle of the same waist

# Include <stdio. h> int main () {int I, j, len; puts ("right angle at the lower left corner of the isosceles right triangle. "); Printf (" side length: "); scanf (" % d ", & len); for (int I = 1; I <= len; I ++) {for (int j = 1; j <= len-I + 1; j ++) {putchar ('*');} putchar ('\ n ');} return 0 ;}

 

 

(4) display the right angleUpper rightRight triangle of the same waist

# Include <stdio. h> int main () {int I, j, len; puts ("right angle at the lower left corner of the isosceles right triangle. "); Printf (" side length: "); scanf (" % d ", & len); for (int I = 1; I <= len; I ++) {for (int j = 1; j <= I; j ++) {putchar ('') ;}for (int j = 1; j <= len-I + 1; j ++) {putchar ('*');} putchar ('\ n');} return 0 ;}

 

 

5. Write a program and enter an integer to display the pyramid shape of the input integer layer.

Tip: line I (I-1) x 2 + 1 '*'

# Include "stdio. h" int main (int argc, char const * argv []) {int n; puts ("Let's plot a pyramid. "); Printf (" the pyramid has several layers: "); scanf (" % d ", & n); for (int I = 1; I <= n; I ++) {for (int j = 1; j <= n-I; j ++) {putchar ('') ;}for (int j = 1; j <= I; j ++) {putchar ('*') ;}for (int j = 1; j <I; j ++) {putchar ('*');} putchar ('\ n');} return 0 ;}

 

Pyramid deformation (1)

# Include <stdio. h> int main () {int I, j, n; puts ("Let's plot a pyramid. "); Printf (" the pyramid has several layers: "); scanf (" % d ", & n); for (I = 1; I <= n; I ++) {for (j = 0; j <n-I; j ++) printf (""); for (j = 1; j <2 * I; j ++) printf ("% d", I); printf ("\ n");} return 0 ;}

 

Pyramid deformation (2)

# Include <stdio. h> int main () {int I, j, n; puts ("Let's plot a pyramid. "); Printf (" the pyramid has several layers: "); scanf (" % d ", & n); for (I = 1; I <= n; I ++) {for (j = 0; j <n-I; j ++) printf (""); for (j = 1; j <= I; j ++) printf ("% d", j); for (j = I-1; j> 0; j --) printf ("% d", j ); printf ("\ n ");}}

 

Summary

Loop is the foundation of C language, so you must practice it well and be skillful. Note:

  • Note that the while statement and the do-while statement are differentiated. The former is judged first and then executed, while the latter is executed first and then judged.
  • The do statement is executed at least once, while the while statement is not executed once.
  • Do not place an empty statement after the for and while statements.
  • The loop body of the do statement, even a single statement, can be enclosed by {} to make it a composite Statement (program block), so that the program will be easy to read.

 

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.