用c语言if函数输入两个整数 输出他们的差值
时间: 2023-04-04 10:03:27 浏览: 197
可以使用以下代码实现:
```c
#include <stdio.h>
int main() {
int a, b;
printf("请输入两个整数:\n");
scanf("%d%d", &a, &b);
if (a > b) {
printf("%d - %d = %d\n", a, b, a - b);
} else {
printf("%d - %d = %d\n", b, a, b - a);
}
return 0;
}
```
以上代码中,使用了 if 语句来判断两个整数的大小关系,从而输出它们的差值。如果第一个整数大于第二个整数,则输出第一个整数减去第二个整数的结果;否则输出第二个整数减去第一个整数的结果。
相关问题
c语言自定义函数输出m和n的差值
以下是一个简单的C语言程序,它定义了一个自定义函数来计算两个整数m和n的差值,并输出结果。
```c
#include <stdio.h>
int diff(int m, int n)
{
return m - n;
}
int main()
{
int m = 10, n = 5;
int result = diff(m, n);
printf("The difference between %d and %d is %d\n", m, n, result);
return 0;
}
```
在这个程序中,我们定义了一个名为`diff`的函数,它接受两个整数参数m和n,并返回它们的差值。在`main`函数中,我们声明了两个整数变量m和n,并将它们初始化为10和5。然后,我们调用`diff`函数来计算它们的差值,并将结果存储在`result`变量中。最后,我们使用`printf`函数将结果输出到控制台。
当我们运行这个程序时,它将输出以下内容:
```
The difference between 10 and 5 is 5
```
这表明我们的自定义函数成功地计算了10和5的差值,并将结果输出到控制台。
使用C语言求小于106的差值为2的素数对的个数。 要求:请使用函数判断一个正整数是否为素数。
题目:使用C语言求小于106的差值为2的素数对的个数。
解法:首先写一个函数来判断一个正整数是否为素数,然后遍历 3 到 106 范围内的所有奇数,判断相邻两个奇数是否都是素数,如果是,则累加素数对的个数即可。
以下是代码实现:
```c
#include <stdio.h>
// 判断一个正整数是否为素数
int is_prime(int n) {
if (n <= 1) {
return 0;
}
int i;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int count = 0; // 素数对的个数
int i;
for (i = 3; i <= 106; i += 2) {
if (is_prime(i) && is_prime(i+2)) {
count++;
}
}
printf("素数对的个数为:%d\n", count);
return 0;
}
```
输出结果为:素数对的个数为:10
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)