C Programming

更新 發佈閱讀 7 分鐘

Section 2 : Switch

Exercise 1: Day of the Week

Take a number representing a day of the week (1 for Monday, 2 for Tuesday, ..., 7 for Sunday) as input. Output the corresponding day's name.

Input Example:

Enter a number (1-7): 3

Output Example:

The day of the week is Wednesday.


Exercise 2: Grade Evaluation

Take a numerical grade as input and output the corresponding letter grade based on the following grading criteria using the switch statement in C. A: 90-100

B: 80-89

C: 70-79

D: 60-69

F: 0-59

Input Example:

Enter your numerical grade: 87

Output example:

Your letter grade is: B






Code Example of Exercise 1

#include <stdio.h>

int main() {
int dayNumber;

// Input day number from the user
printf("Enter a number (1-7): ");
scanf("%d", &dayNumber);

// Using a switch statement to determine the day's name
switch (dayNumber) {
case 1:
printf("The day of the week is Monday.\n");
break;
case 2:
printf("The day of the week is Tuesday.\n");
break;
case 3:
printf("The day of the week is Wednesday.\n");
break;
case 4:
printf("The day of the week is Thursday.\n");
break;
case 5:
printf("The day of the week is Friday.\n");
break;
case 6:
printf("The day of the week is Saturday.\n");
break;
case 7:
printf("The day of the week is Sunday.\n");
break;
default:
printf("Invalid input. Please enter a number between 1 and 7.\n");
}

return 0;
}


Code Example of Exercise 2


#include <stdio.h>

int main() {
int numericalGrade;

// Input numerical grade from the user
printf("Enter your numerical grade: ");
scanf("%d", &numericalGrade);

// Using a switch statement to determine the letter grade
char letterGrade;
switch (numericalGrade / 10) {
case 10:
case 9:
letterGrade = 'A';
break;
case 8:
letterGrade = 'B';
break;
case 7:
letterGrade = 'C';
break;
case 6:
letterGrade = 'D';
break;
default:
letterGrade = 'F';
}

// Output the letter grade
printf("Your letter grade is: %c\n", letterGrade);

return 0;
}

It creates a switch statement that takes the value of numericalGrade / 10 as the controlling expression. This allows to group numerical grades into ranges corresponding to letter grades. This program assumes that the input grade is an integer value between 0 and 100. It rounds down to the nearest 10 to determine the letter grade based on the provided grading criteria.

留言
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/25
C Programming: if else Examples
2023/08/25
C Programming: if else Examples
看更多
你可能也想看
Thumbnail
5 月將於臺北表演藝術中心映演的「2026 北藝嚴選」《海妲・蓋柏樂》,由臺灣劇團「晃晃跨幅町」製作,本文將以從舞台符號、聲音與表演調度切入,討論海妲・蓋柏樂在父權社會結構下的困境,並結合榮格心理學與馮.法蘭茲對「阿尼姆斯」與「永恆少年」原型的分析,理解女人何以走向精神性的操控、毀滅與死亡。
Thumbnail
5 月將於臺北表演藝術中心映演的「2026 北藝嚴選」《海妲・蓋柏樂》,由臺灣劇團「晃晃跨幅町」製作,本文將以從舞台符號、聲音與表演調度切入,討論海妲・蓋柏樂在父權社會結構下的困境,並結合榮格心理學與馮.法蘭茲對「阿尼姆斯」與「永恆少年」原型的分析,理解女人何以走向精神性的操控、毀滅與死亡。
Thumbnail
背景:從冷門配角到市場主線,算力與電力被重新定價   小P從2008進入股市,每一個時期的投資亮點都不同,記得2009蘋果手機剛上市,當時蘋果只要在媒體上提到哪一間供應鏈,隔天股價就有驚人的表現,當時光學鏡頭非常熱門,因為手機第一次搭上鏡頭可以拍照,也造就傳統相機廠的殞落,如今手機已經全面普及,題
Thumbnail
背景:從冷門配角到市場主線,算力與電力被重新定價   小P從2008進入股市,每一個時期的投資亮點都不同,記得2009蘋果手機剛上市,當時蘋果只要在媒體上提到哪一間供應鏈,隔天股價就有驚人的表現,當時光學鏡頭非常熱門,因為手機第一次搭上鏡頭可以拍照,也造就傳統相機廠的殞落,如今手機已經全面普及,題
Thumbnail
這是一場修復文化與重建精神的儀式,觀眾不需要完全看懂《遊林驚夢:巧遇Hagay》,但你能感受心與土地團聚的渴望,也不急著在此處釐清或定義什麼,但你的在場感受,就是一條線索,關於如何找著自己的路徑、自己的聲音。
Thumbnail
這是一場修復文化與重建精神的儀式,觀眾不需要完全看懂《遊林驚夢:巧遇Hagay》,但你能感受心與土地團聚的渴望,也不急著在此處釐清或定義什麼,但你的在場感受,就是一條線索,關於如何找著自己的路徑、自己的聲音。
Thumbnail
題目:如果提供的數字在0-9之間,請以文字形式返回。輸入1、輸出 “One”
Thumbnail
題目:如果提供的數字在0-9之間,請以文字形式返回。輸入1、輸出 “One”
Thumbnail
本文分析導演巴里・柯斯基(Barrie Kosky)如何運用極簡的舞臺配置,將布萊希特(Bertolt Brecht)的「疏離效果」轉化為視覺奇觀與黑色幽默,探討《三便士歌劇》在當代劇場中的新詮釋,並藉由舞臺、燈光、服裝、音樂等多方面,分析該作如何在保留批判核心的同時,觸及觀眾的觀看位置與人性幽微。
Thumbnail
本文分析導演巴里・柯斯基(Barrie Kosky)如何運用極簡的舞臺配置,將布萊希特(Bertolt Brecht)的「疏離效果」轉化為視覺奇觀與黑色幽默,探討《三便士歌劇》在當代劇場中的新詮釋,並藉由舞臺、燈光、服裝、音樂等多方面,分析該作如何在保留批判核心的同時,觸及觀眾的觀看位置與人性幽微。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News