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
3會員
5內容數
留言0
查看全部
avatar-img
發表第一個留言支持創作者!
邵的沙龍 的其他內容
你可能也想看
Google News 追蹤
Thumbnail
題目敘述 Integer to English Words 給定一個整數num 請轉換成對應的的英文數字表達(One, Two, Three, ... 那種數字表達式)
問題 Write a function expect that helps developers test their code. It should take in any value val and return an object with the following two functio
Thumbnail
題目敘述 題目會給定一個整數陣列arr,要求我們判斷是否每個元素的出現次數都不同? 題目的原文敘述 測試範例 Example 1: Input: arr = [1,2,2,1,1,3] Output: true Explanation: The value 1 has 3 occurre
Thumbnail
IF,Switch,三元運算子語法說明 IF條件選擇結構說明 IF為布林條件,當()內條件式滿足True執行if區塊的程式碼,不滿足則執行else區塊的程式碼,若無else也行。
Thumbnail
題目敘述 Integer to English Words 給定一個整數num 請轉換成對應的的英文數字表達(One, Two, Three, ... 那種數字表達式)
問題 Write a function expect that helps developers test their code. It should take in any value val and return an object with the following two functio
Thumbnail
題目敘述 題目會給定一個整數陣列arr,要求我們判斷是否每個元素的出現次數都不同? 題目的原文敘述 測試範例 Example 1: Input: arr = [1,2,2,1,1,3] Output: true Explanation: The value 1 has 3 occurre
Thumbnail
IF,Switch,三元運算子語法說明 IF條件選擇結構說明 IF為布林條件,當()內條件式滿足True執行if區塊的程式碼,不滿足則執行else區塊的程式碼,若無else也行。