1、单分支结构语句
用单分支结构进行奇偶判断:
#include <stdio.h>int main() {int num;printf("Please enter an integer: ");scanf("%d", &num);if (num % 2 != 0) {printf("%d is odd! \n", num);}if (num % 2 == 0) {printf("%d is even! \n", num);}return 0; }
- 运行结果:
2、双分支结构语句
用双分支结构进行奇偶判断:
#include <stdio.h>int main() {int num;printf("Please enter an integer: ");scanf("%d", &num);if (num % 2 != 0) {printf("%d is odd! \n", num);} else {printf("%d is even! \n", num);}return 0; }
- 运行结果:
3、多分支结构语句
用多分支结构进行学生成绩的判断:
#include <stdio.h>int main() {int score;printf("Please enter score: ");scanf("%d", &score);if (score >= 90) {printf("A\n");} else if (score >= 80) {printf("good\n");} else if (score >= 70) {printf("medium\n"); } else if (score >= 60) {printf("pass\n");} else {printf("fail");}return 0; }
- 运行结果:
4、分支语句的嵌套
判断某学生成绩 score 是否及格:
#include <stdio.h>int main() {int score;printf("Please enter score: ");scanf("%d", &score);if (score >= 60) {if (score >= 90) {printf("A\n");} else {printf("pass\n");}} else {printf("fail\n");}return 0; }
- 运行结果:
5、switch 选择语句
输入一个简单的四则运算表达式,包含两个实数和一个运算符,输出计算结果:
#include <stdio.h>int main() {char op;double x, y;printf("Please enter an expression: ");scanf("%lf %c %lf", &x, &op, &y);switch (op) {case '+':printf("The expression results in: %.2f\n", x + y);break;case '-':printf("The expression results in: %.2f\n", x - y);break;case '*':printf("The expression results in: %.2f\n", x * y);break;case '/':printf("The expression results in: %.2f\n", x / y);break;default:printf("Operator error! \n");}return 0; }
- 运算结果:
6、while 循环结构与执行流程
用 while 循环语句计算输入的 10 个整数和奇数的和:
#include <stdio.h>int main() {int num;int i = 1, sum = 0;int count = 0;printf("Please enter 10 integers at a time: ");while (i <= 10) {scanf("%d", &num);if (num % 2 != 0) {sum += num;count++;}i++;}if (count == 0) {printf("There are no odd numbers in the data!");} else {printf("Odd numbers are: %d\n", count);printf("Odd sum: %d", sum);}return 0; }
- 运算结果:
7、for 循环语句与执行流程
打印出所有”水仙花数“:
#include <stdio.h>int main() {int a, b, c;int i;for (i = 100; i < 1000; i++) {a = i % 10;b = (i / 10) % 10;c = i / 100;if (a * a * a + b * b * b + c * c * c == i) {printf("%d\t", i);}}return 0; }
- 运行结果:
用嵌套 for 循环方式计算 1 到 100 的素数和:
#include <stdio.h>int main() {int sum = 0, i = 0, j = 0;int count;printf("The prime numbers from 1 to 100 are: ");for (i = 2; i <= 100; i++) {count = 0;for (j = 2; j <= i / 2; j++) {if (i % j == 0) {count++;break;}}if (count == 0) {printf("%d ", i);sum += i;}}printf("\nThe sum of primes from 1 to 100 is: %d", sum);return 0; }
- 运算结果:
images%5Cimage-20241107233301176.png&pos_id=img-piAAi8Jj-1731048018050)
8、do-while 循环结果与执行流程
计算两个数的最大公约数
#include <stdio.h>int main() {int m, n, r, t;int m1, n1;printf("Please enter the first number: ");scanf("%d", &m);printf("Please enter the second number: ");scanf("%d", &n);m1 = m;n1 = n;if (m < n) {t = m;m = n;n = t;}do {r = m % n;m = n;n = r;} while (r != 0);printf("The greatest common divisor of %d and %d is %d\n", m1, n1, m);return 0; }
- 运行结果:
9、循环结构嵌套
九九乘法表的打印:
#include <stdio.h>int main() {int i = 0, j = 0;printf("*");for (i = 1; i <= 9; i++) {printf("%8d", i);}printf("\n");for (i = 1; i <= 9; i++) {printf("%d", i);for (j = 1; j <= i; j++) {printf("%3d*%d=%2d", i, j, i * j);}printf("\n");}return 0; }
- 运行结果:
10、辅助语句 break 和 continue
输入大于 2 的整数,判断该数是否为素数:
#include <stdio.h>int main() {int m, i, flag;flag = 1;printf("Please enter an integer greater than 2: ");scanf("%d", &m);for (i = 2; i < m; i++) {if (m % i == 0) {flag = 0;break;}}if (flag) {printf("%d is prime number! \n", m);} else {printf("%d is not prime number! \n", m);}return 0; }
- 运行结果为
判断 1800 年到 2024 年之间的所有闰年并输出:
#include <stdio.h>int main() {int year;for (year = 1800; year <= 2024; year++) {if (!(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {continue;}printf("%d ", year);if (year % 10 == 0) {printf("\n");}}return 0; }
- 运行结果:
11、改良的计算器
改良版实现四则运算功能的计算器:
#include <stdio.h>int main() {double displayed_value;double new_entry;char command_character;printf("Improved computer program\n");printf("Tip: (number> )Enter number, (command> )Enter Operator +、-、*、/\n");printf("number> ");scanf("%lf", &displayed_value);getchar();printf("command> ");scanf("%c", &command_character);while (command_character != 'Q') {switch (command_character) {case 'c':case 'C':scanf("%lf", &displayed_value);break;case '+':printf("number> ");scanf("%lf", &new_entry);displayed_value += new_entry;break;case '-':printf("number> ");scanf("%lf", &new_entry);displayed_value -= new_entry;break;case 'x':case 'X':case '*':printf("number> ");scanf("%lf", &new_entry);displayed_value *= new_entry;break;case '/':printf("number> ");scanf("%lf", &new_entry);while (new_entry == 0) {printf("Divisor cannot be 0, please re-enter! \n");printf("number> ");scanf("%lf", &new_entry);}displayed_value /= new_entry;break;default:printf("Invalid input, please re-enter the command type! \n");}printf("Value: %lf\n", displayed_value);getchar();printf("command> ");scanf("%c", &command_character);}return 0; }
- 运算结果: