Practice Problem 10
Basics
Question 1-1
What is the name for a loop where the condition is checked first, like in a while statement?
Question 1-2
What is the term for a loop where the condition is checked after execution, like a do-while loop?
Program reading
What program is intended to display what?
Determine the answer based on the processing content and variable names.
Determine the answer based on the processing content and variable names.
Question 2-1
#include <stdio.h>
int main(void)
{
int year = 0;
double money = 10000;
while (money < 15000) {
year++;
money *= 1.01;
}
printf("%d , %f\n", year, money);
return 0;
}
Program Manual
Question 3-1
I want to create a program to input test scores. However, since test scores can only range from 0 to 100, if any other value is entered, the program should prompt the user to re-enter it.
explanatory
Question 4-1
Briefly explain why do-while loops are well-suited for input validation.
Fundamentals (Answer Key)
Solution 1-1
Preliminary screening
Solution 1-2
Post-assessment
Program Reading (Solution Example)
Solution 2-1
A program that displays how many years it takes for 10,000 yen deposited in a bank with an annual interest rate of 1% to grow to 15,000 yen.
Program Documentation (Example Solution)
Solution 3-1
#include <stdio.h>
int main(void)
{
int score;
do {
printf("Please enter the score.:");
scanf("%d", &score);
} while (score < 0 || score > 100);
printf("Input score %d\n", score);
return 0;
}
If you want to display a message when re-entering data, do the following.
The initial input is distinguished by checking if the variable 'score' is equal to 0.
If a score of 0 is entered, the loop will exit, so the conditions won't overlap.
Answer 3-1 Solution 2
#include <stdio.h>
int main(void)
{
int score = 0;
do {
if (score != 0)
printf("Please enter scores in the range of 0 to 100.\n");
printf("Please enter the score.:");
scanf("%d", &score);
} while (score < 0 || score > 100);
printf("Input score %d\n", score);
return 0;
}
Question
Descriptive (answer example)
Solution 4-1
The do-while statement is a post-condition loop, so it always executes at least once initially, preventing situations where no input is entered.
About This Site
Learning C language through suffering (Kushi C) isThis 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.




