比較三個整數(shù)變量是您可以很容易編寫的一個簡單的程序之一。 在這個程序中,您可以使用scanf()函數(shù)從用戶處獲取輸入,也可以在程序本身中靜態(tài)定義。
我們期望它也是一個簡單的程序,將一個值與其余兩個進行比較,并檢查結(jié)果,并對所有變量應用相同的過程。 對于此程序,所有值應該是唯一的值。
我們可以為下面給出的程序繪制一個流程圖 -

該圖中顯示了三個if-else-if和另一個對比語句。
下面我們來看看這個程序的具體實現(xiàn) -
#include <stdio.h>
int main() {
int a, b, c;
a = 10;
b = 20;
c = 30;
if ( a > b && a > c )
printf("%d is the largest.", a);
else if ( b > a && b > c )
printf("%d is the largest.", b);
else if ( c > a && c > b )
printf("%d is the largest.", c);
else
printf("Values are not unique");
return 0;
}
執(zhí)行上面示例代碼,得到以下結(jié)果 -
30 is the largest.