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;
}


留言
avatar-img
留言分享你的想法!
avatar-img
邵的沙龍
3會員
5內容數
邵的沙龍的其他內容
2023/08/27
C Programming: If-Else, For Loop Exercises
2023/08/27
C Programming: If-Else, For Loop Exercises
2023/08/26
C program: For Loop
2023/08/26
C program: For Loop
2023/08/26
C programming : Switch
2023/08/26
C programming : Switch
看更多
你可能也想看
Thumbnail
作者分享自己曾在蝦皮購買的好物,與介紹蝦皮雙12購物節的優惠活動,以及自己打算入手的商品,也詳細說明如何透過「蝦皮分潤計畫」創造額外的被動收入,鼓勵讀者一同參與。
Thumbnail
作者分享自己曾在蝦皮購買的好物,與介紹蝦皮雙12購物節的優惠活動,以及自己打算入手的商品,也詳細說明如何透過「蝦皮分潤計畫」創造額外的被動收入,鼓勵讀者一同參與。
Thumbnail
交換禮物總是讓人又愛又怕?Ruby整理蝦皮500元內必買清單,從養生茶包、心靈牌卡到可愛環保杯、公益零錢包,送禮自用兩相宜。同時分享「蝦皮分潤計畫」教學,購買好物的同時還能賺零用錢,雙12購物攻略一次看!
Thumbnail
交換禮物總是讓人又愛又怕?Ruby整理蝦皮500元內必買清單,從養生茶包、心靈牌卡到可愛環保杯、公益零錢包,送禮自用兩相宜。同時分享「蝦皮分潤計畫」教學,購買好物的同時還能賺零用錢,雙12購物攻略一次看!
Thumbnail
冬天到了,又到了我最愛窩在家網購的季節!從原本對網拍沒興趣,到現在成為蝦皮的忠實用戶,這個轉變連我自己都覺得有趣。這次趁著蝦皮雙12活動,想跟大家分享我最近入手的冬季養生好物——艾灸罐,還有其他實用的生活小物。文末也會介紹蝦皮分潤計畫,讓常網購的你,也能把購物經驗變成額外收入!
Thumbnail
冬天到了,又到了我最愛窩在家網購的季節!從原本對網拍沒興趣,到現在成為蝦皮的忠實用戶,這個轉變連我自己都覺得有趣。這次趁著蝦皮雙12活動,想跟大家分享我最近入手的冬季養生好物——艾灸罐,還有其他實用的生活小物。文末也會介紹蝦皮分潤計畫,讓常網購的你,也能把購物經驗變成額外收入!
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用2^k 的形式來表達 (二的冪)?
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用2^k 的形式來表達 (二的冪)?
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News