learn through suffering C language learn through suffering 
C language

Practice Problem 8

Basics


Question 1-1
What is the option to add a statement to an if statement that executes when the condition is false?


Question 1-2
What is the colloquial term for combining multiple sentences like the one above?


Question 1-3
What is the sentence used to separate sentences to be processed by number?

Program reading

What program is intended to display what?
Determine the answer based on the processing content and variable names.

Question 2-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 Manual


Question 3-1
Create a program that displays the corresponding lunar calendar month when a month is entered.
Additionally, display some kind of message when a non-existent month is entered.

Hint: The lunar calendar months are sequentially numbered starting from January as follows:
Mutsuki, Kisaragi, Yayoi, Uzuki, Satsuki, Minazuki, Fumizuki, Hazuki, Nagatsuki, Kannazuki, Shimotsuki, and Shiwasu.

explanatory


Question 4-1
Why is a break statement placed after each case statement in a switch statement? Also, briefly state in what cases this is unnecessary.

Fundamentals (Answer Key)


Solution 1-1
else statement


Solution 1-2
else-if statement


Solution 1-3
switch statement or switch-case statement

Program Reading (Solution Example)


Solution 2-1
Enter a year, and I will tell you if the Olympic Games were held that year.

This program rewrites the problem from the previous chapter using if-else statements.

Program Documentation (Example 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("January\n");
        break;
    case 2:
        printf("February\n");
        break;
    case 3:
        printf("March\n");
        break;
    case 4:
        printf("April\n");
        break;
    case 5:
        printf("June\n");
        break;
    case 6:
        printf("July\n");
        break;
    case 7:
        printf("August\n");
        break;
    case 8:
        printf("August\n");
        break;
    case 9:
        printf("September\n");
        break;
    case 10:
        printf("October\n");
        break;
    case 11:
        printf("November\n");
        break;
    case 12:
        printf("December\n");
        break;
    default:
        printf("There is no such month.。\n");
        break;
    }

    return 0;
}

While an if statement is also correct, a switch statement is more suitable when dealing with numerous branches numbered sequentially.
Points will be deducted if the default statement is missing.
Please be sure to remember the break statement.

Descriptive (answer example)


Solution 4-1
The switch statement only has the functionality to jump to the corresponding case statement. Therefore, a break statement must be added at the end of each case statement to exit the entire switch statement. However, it is acceptable to chain them together only when the processing within each case statement shares common elements.



About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.


Part 0: Program Overview

  1. What is a program?



Chapter 3: Displaying on the Screen

  1. String Display
  2. line break
  3. Practice Problem 3

Chapter 4: Displaying and Calculating Numbers

  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4


Chapter 6: Input from the Keyboard

  1. input function
  2. The fear of input
  3. Practice Problem 6



Chapter 9: Repeating a Fixed Number of Times

  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9

Chapter 10: Repeating Without Knowing the Number of Times

  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10



Chapter 13: Handling Multiple Variables at Once

  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13






Chapter 19: Dynamic Arrays

  1. Create arrays freely.
  2. Practice Problem 19

Loading comment system...