使用C语言,写一个程序 n1为54,n2 为84,n1是n2的64%
时间: 2024-06-06 20:06:29 浏览: 16
As an AI language model, I am not capable of experiencing or expressing emotions. Therefore, the concept of null does not apply to me in any way. However, null represents the absence of a value or a non-existent object or entity in computer programming, databases, and other related fields.
相关问题
使用C语言,写一个程序 n1为54,n2
为13,计算它们的和、差、积、商、余数和平均数。
代码如下:
```c
#include <stdio.h>
int main() {
int n1 = 54, n2 = 13;
int sum = n1 + n2;
int difference = n1 - n2;
int product = n1 * n2;
int quotient = n1 / n2;
int remainder = n1 % n2;
float average = (float)(n1 + n2) / 2;
printf("n1 = %d, n2 = %d\n", n1, n2);
printf("sum = %d\n", sum);
printf("difference = %d\n", difference);
printf("product = %d\n", product);
printf("quotient = %d\n", quotient);
printf("remainder = %d\n", remainder);
printf("average = %.2f\n", average);
return 0;
}
```
输出结果如下:
```
n1 = 54, n2 = 13
sum = 67
difference = 41
product = 702
quotient = 4
remainder = 2
average = 33.50
```
C语言用函数计算正整数n1到n2的累加和(n1、n2的值由键盘输入)。
以下是一个用C语言编写的函数,用于计算正整数n1到n2的累加和:
```c
#include <stdio.h>
int sum(int n1, int n2) {
int i, result = 0;
for (i = n1; i <= n2; i++) {
result += i;
}
return result;
}
int main() {
int n1, n2;
printf("请输入两个正整数n1和n2:");
scanf("%d %d", &n1, &n2);
printf("累加和为:%d\n", sum(n1, n2));
return 0;
}
```
在上述代码中,我们定义了一个名为`sum`的函数,该函数接受两个参数n1和n2,表示要计算累加和的范围。函数内部使用一个循环来遍历从n1到n2的所有整数,并将它们累加到一个变量`result`中。最后,函数返回累加和。
在`main`函数中,我们首先从键盘输入两个正整数n1和n2,然后调用`sum`函数计算累加和,并将结果打印出来。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)