C Programming: if else Examples

閱讀時間約 5 分鐘

Code Example of Exercise 1: Even or odd

In this exercise, the program prompts the user to enter an integer. It then uses the modulo operator % to check whether the number is divisible evenly by 2. If the remainder is 0, the number is even; otherwise, it's odd.

#include <stdio.h>

int main() {
int number;

// Input the number from the user
printf("Enter an integer: ");
scanf("%d", &number);

// Check if the number is even or odd
if (number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}

return 0;
}


Code Example of Exercise 2: Grading System

#include <stdio.h>

int main() {
int score;

printf("Enter the student's score: ");
scanf("%d", &score);

if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else if (score >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}

return 0;
}


Exercise 3: Calculator Program

In this exercise, the program takes two numbers and an operator as input and uses "if-else" statements to perform the corresponding arithmetic operation. It handles division by zero as well as invalid operators.

int main() {
float num1, num2;
char operator;

printf("Enter the first number: ");
scanf("%f", &num1);

printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);

printf("Enter the second number: ");
scanf("%f", &num2);

if (operator == '+') {
printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2);
} else if (operator == '-') {
printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2);
} else if (operator == '*') {
printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2);
} else if (operator == '/') {
if (num2 != 0) {
printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2);
} else {
printf("Cannot divide by zero.\n");
}
} else {
printf("Invalid operator.\n");
}

return 0;
}


3會員
5Content count
留言0
查看全部
發表第一個留言支持創作者!
邵的沙龍 的其他內容
你可能也想看
Google News 追蹤
Thumbnail
本專欄將提供給您最新的市場資訊、產業研究、交易心法、優質公司介紹,以上內容並非個股分析,還請各位依據自身狀況作出交易決策。歡迎訂閱支持我,獲得相關內容,也祝您的投資之路順遂! 每年 $990 訂閱方案👉 https://reurl.cc/VNYVxZ 每月 $99 訂閱方案👉https://re
Thumbnail
條件句在編碼上隨處可見. 程式編碼就是一堆分別判斷情況, 再提出行動指令的邏輯. if為邏輯的根本. 就好像一個人去計劃做一件事或對一件事作預備, 不可能無假設, 沒有如果這字眼, 不用if這字眼, 就無法把情況判斷, 分類, 也無沒寫程式. 總而言之, 學條件句為最基本而必要. 其他相關:
Thumbnail
只要你肯聯繫我,我就願意再勇敢一次, 重道覆轍也沒關係, 就算你是陷阱、是無底深淵、是深不見底的大海, 只要是你,我願意跳下去, 願意沈溺於一片以你為名的深海。 我站在風口,整個世界都是你的氣息。 我將滿懷愛意的等待下個盛夏的風, 和遠方的你。 你知道 C-14 的半衰期有多久嗎? 它不及我在冥冥之
Thumbnail
Ironman伴讀小書僮又來了! 今天要念英文繪本給Piupiu和Piu 哥聽。 防疫期間在家育兒真的是很挑戰英雄,還是拯救地球簡單一點~ 跳一下來到繪本介紹, A Chair for My Mother -by Vera B. Williams 這本就是勸生女兒的繪本呀!故事中的小女孩孝順又貼心,
Thumbnail
int main()、註解//、include 、命名空間、using namespace
string SrcStr = "admin"; string[] UserNameCvt = SrcStr.Split(','); Roles.AddUsersToRole(UserNameCvt, "Administrator"); 註記: 網路上較少此種例子,多為string[] to S
Thumbnail
可能包含敏感內容
如果你有關注台灣媒體,就會知道安心亞是全台首位公開穿C字褲的女藝人,那時才2009年,跟現在相比之下算是民風更為保守的年代,一方面真的很佩服她的勇氣,另一方面又會覺得:穿C字褲有什麼好處呢?其實C字褲並不是大眾印象中跟情色畫上等號的東西,反而像是丁字褲的升級版而已,都能拿來做隱藏痕跡使用。希望這篇文
Thumbnail
C世代(Connected Generation, 連接世代)是美容保養品-網購的主力──了解他們的特質,才能讓保養品網路行銷大賣... 隨著智慧裝置普及,全球「網路購物」市場高速增長(根據eMarketer統計:全球零售電子商務銷售額,2017年為1.357萬億美元,預估至2021年將達3.554
Thumbnail
本專欄將提供給您最新的市場資訊、產業研究、交易心法、優質公司介紹,以上內容並非個股分析,還請各位依據自身狀況作出交易決策。歡迎訂閱支持我,獲得相關內容,也祝您的投資之路順遂! 每年 $990 訂閱方案👉 https://reurl.cc/VNYVxZ 每月 $99 訂閱方案👉https://re
Thumbnail
條件句在編碼上隨處可見. 程式編碼就是一堆分別判斷情況, 再提出行動指令的邏輯. if為邏輯的根本. 就好像一個人去計劃做一件事或對一件事作預備, 不可能無假設, 沒有如果這字眼, 不用if這字眼, 就無法把情況判斷, 分類, 也無沒寫程式. 總而言之, 學條件句為最基本而必要. 其他相關:
Thumbnail
只要你肯聯繫我,我就願意再勇敢一次, 重道覆轍也沒關係, 就算你是陷阱、是無底深淵、是深不見底的大海, 只要是你,我願意跳下去, 願意沈溺於一片以你為名的深海。 我站在風口,整個世界都是你的氣息。 我將滿懷愛意的等待下個盛夏的風, 和遠方的你。 你知道 C-14 的半衰期有多久嗎? 它不及我在冥冥之
Thumbnail
Ironman伴讀小書僮又來了! 今天要念英文繪本給Piupiu和Piu 哥聽。 防疫期間在家育兒真的是很挑戰英雄,還是拯救地球簡單一點~ 跳一下來到繪本介紹, A Chair for My Mother -by Vera B. Williams 這本就是勸生女兒的繪本呀!故事中的小女孩孝順又貼心,
Thumbnail
int main()、註解//、include 、命名空間、using namespace
string SrcStr = "admin"; string[] UserNameCvt = SrcStr.Split(','); Roles.AddUsersToRole(UserNameCvt, "Administrator"); 註記: 網路上較少此種例子,多為string[] to S
Thumbnail
可能包含敏感內容
如果你有關注台灣媒體,就會知道安心亞是全台首位公開穿C字褲的女藝人,那時才2009年,跟現在相比之下算是民風更為保守的年代,一方面真的很佩服她的勇氣,另一方面又會覺得:穿C字褲有什麼好處呢?其實C字褲並不是大眾印象中跟情色畫上等號的東西,反而像是丁字褲的升級版而已,都能拿來做隱藏痕跡使用。希望這篇文
Thumbnail
C世代(Connected Generation, 連接世代)是美容保養品-網購的主力──了解他們的特質,才能讓保養品網路行銷大賣... 隨著智慧裝置普及,全球「網路購物」市場高速增長(根據eMarketer統計:全球零售電子商務銷售額,2017年為1.357萬億美元,預估至2021年將達3.554