C语言判断三个数相等
时间: 2024-10-08 12:12:34 浏览: 42
C语言判断三角形
在C语言中,判断三个整数是否相等,通常会使用一个临时变量或者直接比较它们的值。以下是两种常见的做法:
1. 使用临时变量法:
```c
#include <stdio.h>
int main() {
int num1, num2, num3, temp;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
temp = num1;
if (temp == num2) {
if (temp == num3) {
printf("All numbers are equal.\n");
} else {
printf("The first two numbers are equal, but different from the third.\n");
}
} else {
printf("The first two numbers are not equal.\n");
}
return 0;
}
```
2. 直接比较法(适用于数值较小的情况,避免溢出):
```c
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 == num2 && num2 == num3) {
printf("All numbers are equal.\n");
} else {
printf("Numbers are not all equal.\n");
}
return 0;
}
```
这两种方法都可以用来判断三个数是否相等,根据具体情况选择合适的方式。
阅读全文