C language learned by suffering
C language learned by suffering
Exercise 8
fundamental knowledge
Q1-1
What is the option to add a statement to the if statement to be executed when the condition is false?
Q 1-2
What is commonly called a writing style that uses a combination of several of the above sentences?
Q1-3
What statement is used when you want to separate the statements to be processed by number?
program read-only
What is the next program to display?
Answer by judging from the process contents and variable names.
Answer by judging from the process contents and variable names.
Q2-1
#include <stdio.h>
int main(void)
{
int year;
scanf("%d", &year);
if (year % 2 == 0) {
if (year % 4 == 0) {
printf("summer\n");
} else {
printf("winter\n");
}
} else {
printf("do not\n");
}
return 0;
}
program writing
Q3-1
Create a program that displays the corresponding month of the lunar calendar when a month is entered.
Also, display a message when a non-existent month is entered.
Hint: The lunar calendar months are listed in order from January.
Mutsuki, Kisaragi, Yayoi, Ugetsu, Satsuki, Mizunazuki, Bunzuki, Hazuki, Nagatsuki, Kanashizuki, Shimotsuki, Shihatsu
Also, display a message when a non-existent month is entered.
Hint: The lunar calendar months are listed in order from January.
Mutsuki, Kisaragi, Yayoi, Ugetsu, Satsuki, Mizunazuki, Bunzuki, Hazuki, Nagatsuki, Kanashizuki, Shimotsuki, Shihatsu
descriptive expression
Q4-1
In a switch statement, why is a break statement placed after each case statement?
Also, briefly explain when this is unnecessary.
Also, briefly explain when this is unnecessary.
Basic Knowledge (sample answers)
Solution 1-1
else statement
Solution 1-2
else-if statement
Solution 1-3
switch statement or switch to case statement
Program reading (example solution)
Solution 2-1
Enter a year in the Western calendar year and it will indicate whether the Olympics will be held or not.
*This program rewrites the problem in the previous chapter with if-else statements.
Program writing (example of solution)
Solution 3-1
#include <stdio.h>
int main(void)
{
int month;
printf("Please enter the month.") ;
scanf("%d", &month);
switch (month) {
case 1:
printf("睦月\n");
break;
case 2:
printf("如月\n"); break; case 2: printf("如月\n");
break;
case 3:
printf("Yayoi\n");
break;
case 4:
printf("卯月\n"); break; case 4: printf("卯月\n");
break;
case 5:
printf("皐月\n"); break; case 6: printf("皐月\n")
break;
case 6:
printf("水無月\n"); break; case 6: printf("水無月\n")
break;
case 7:
printf("文月\n"); break; case 7: printf("文月\n")
break;
case 8:
printf("葉月\n");
break;
case 9:
printf("長月\n");
break;
case 10:
printf("神無月\n"); break; case 10: printf("神無月\n")
break;
case 11:
printf("霜月\n"); break; case 11: printf("神無月\n")
break;
case 12:
printf("師走\n");
break;
default:
printf("No such month. \n");
break;
}
return 0;
}
The *if statement is also correct, but the switch statement is appropriate for numerous branches by number.
*Points will be deducted if a default statement is missing.
Be careful not to forget the *break statement.
Short Answer Type (Sample Answer)
Solution 4-1
Since the switch statement only has the ability to jump to the corresponding numbered case statement, the
It is necessary to add a break statement so that at the end of each CASE statement, the entire SWITCH statement is left out.
However, only when there is a commonality in the processing of each case statement, it is acceptable to connect them as they are.
It is necessary to add a break statement so that at the end of each CASE statement, the entire SWITCH statement is left out.
However, only when there is a commonality in the processing of each case statement, it is acceptable to connect them as they are.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.