#include <stdio.h>
int main() {
int a, b;
// 提示使用者輸入資料
printf("Please input two integers:");
scanf("%d%d", &a, &b);
if (a > b) {
printf("%d > %d\n", a, b);
} else if (a < b) {
printf("%d < %d\n", a, b);
} else {
printf("%d == %d\n", a, b);
}
}
檢查使用者輸入的數字是否為0
#include <stdio.h>
int main() {
int num = 100;
printf("Please input an integer:"); // 提示使用者輸入資料
scanf("%d", &num); // 輸入數字到num變數
if (num == 0) {
printf("您輸入的數字是0\n");
} else {
printf("您輸入的數字不是0\n");
}
}
判斷西元年份是否為閏年
判斷是否為閏年的流程圖如下:
依照上述流程圖,可寫出如下的程式
#include <stdio.h>
int main() {
int year;
printf("Please input an the year:"); // 提示使用者輸入資料
scanf("%d", &year); // 輸入年份year
if (year%4 == 0) {
if (year%100 == 0) {
if (year%400 == 0) {
printf("%d是閏年\n", year);
} else {
printf("%d不是閏年\n", year);
}
} else {
printf("%d是閏年\n", year);
}
} else {
printf("%d不是閏年\n", year);
}
}
如果我們把所有可得到閏年的路徑,集合在一起,則更簡潔的寫法是
#include <stdio.h>
int main() {
int year;
printf("Please input the year:"); // 提示使用者輸入資料
scanf("%d", &year); // 輸入年份year
if (year%400 == 0 || year%4 == 0 && year%100 != 0) {
printf("%d是閏年\n", year);
} else {
printf("%d不是閏年\n", year);
}
}
如何求一元二次方程式的解
我們可以畫出如下的流程圖,如何按照此流程圖寫出程式?
請各位讀者自己寫寫看了
2. switch statement
switch (運算式) {
case 常數1:
statement1;
break;
case 常數2:
statement2;
break;
default:
statement;
}
switch (intnum) {
case 1:
printf("The number is one");
break;
case 2:
printf("The number is two");
break;
default:
printf("The number is not one or two");
}