熟悉上机环境和顺序结构编程练习
时间: 2024-01-21 19:17:57 浏览: 32
练习1:编写一个程序,输入两个整数,并输出它们的和、差、积、商和余数。
```
#include <stdio.h>
int main()
{
int a, b;
printf("请输入两个整数:");
scanf("%d %d", &a, &b);
printf("它们的和是:%d\n", a + b);
printf("它们的差是:%d\n", a - b);
printf("它们的积是:%d\n", a * b);
printf("它们的商是:%d\n", a / b);
printf("它们的余数是:%d\n", a % b);
return 0;
}
```
练习2:编写一个程序,输入三个整数,并输出其中的最大值。
```
#include <stdio.h>
int main()
{
int a, b, c, max;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
printf("最大值是:%d\n", max);
return 0;
}
```
练习3:编写一个程序,输入一个字符,判断它是大写字母、小写字母还是数字,并输出相应的提示信息。
```
#include <stdio.h>
int main()
{
char ch;
printf("请输入一个字符:");
scanf("%c", &ch);
if (ch >= 'A' && ch <= 'Z') {
printf("它是一个大写字母。\n");
} else if (ch >= 'a' && ch <= 'z') {
printf("它是一个小写字母。\n");
} else if (ch >= '0' && ch <= '9') {
printf("它是一个数字。\n");
} else {
printf("它不是一个字母或数字。\n");
}
return 0;
}
```
练习4:编写一个程序,输入一个正整数n,计算并输出1到n之间所有奇数的和。
```
#include <stdio.h>
int main()
{
int n, sum = 0;
printf("请输入一个正整数:");
scanf("%d", &n);
for (int i = 1; i <= n; i += 2) {
sum += i;
}
printf("1到%d之间所有奇数的和是:%d\n", n, sum);
return 0;
}
```
练习5:编写一个程序,输入一个正整数n,计算并输出1到n之间所有能被3整除的数的和。
```
#include <stdio.h>
int main()
{
int n, sum = 0;
printf("请输入一个正整数:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
if (i % 3 == 0) {
sum += i;
}
}
printf("1到%d之间所有能被3整除的数的和是:%d\n", n, sum);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)