0% found this document useful (0 votes)
11 views9 pages

02 Control Flow Statements

The document outlines a lab assignment for CSC10012 focused on control flow statements, featuring a series of programming problems. These problems cover conditional statements, loop statements, and various mathematical and logical computations, such as calculating academic performance, determining triangle types, and simulating a simple calculator. Each problem includes specific requirements and examples to guide students in their programming tasks.

Uploaded by

phuctran180406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

02 Control Flow Statements

The document outlines a lab assignment for CSC10012 focused on control flow statements, featuring a series of programming problems. These problems cover conditional statements, loop statements, and various mathematical and logical computations, such as calculating academic performance, determining triangle types, and simulating a simple calculator. Each problem includes specific requirements and examples to guide students in their programming tasks.

Uploaded by

phuctran180406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CSC10012

LAB 02 – CONTROL FLOW STATEMENTS


FIT-HCMUS

Part 1 – Conditional Statements

Problem 1
Write a program to input hours, minutes, and seconds and perform a validation check on the input data.

Problem 2
Write a program that calculates a student’s academic performance based on their scores in Math, Physics, and
Chemistry. The program should prompt the user to enter the student’s scores in Math, Physics, and Chemistry.
The program then calculates the total score of all three subjects and prints a message indicating whether the student
passed or failed. If the student passed, an additional message indicating whether they studied all subjects equally
or unequally.

• If the total score is greater than or equal to 15 and no subject score is below 4, the student passes.

• If the total score is less than 15, the student fails.

• If all subject scores are greater than 5, the student is studying all subjects equally.

• If at least one subject score is below 5, the student is studying all subjects unequally.

Problem 3
Write a program to input a positive integer n and then print whether n is a perfect square. A perfect square is a
number whose square root is an integer.

Problem 4
Write a program to input an integer n and then print "Even number" if n is even; otherwise, print "Odd number".

Problem 5
Write a program that inputs a month and a year. The program then performs the following requirements:
• Check if the given year is a leap year. A leap year is divisible by 4 but not by 100, or is divisible by 400.

• Output the number of days in the given month. Months with 31 days: 1, 3, 5, 7, 8, 10, 12; months with 30
days: 4, 6, 9, 11; February has 28 or 29 days.

1
Problem 6
Write a program to input a number from 0 to 9 and print the corresponding word. For example, if the input is 3,
the output should be "Three" (using a switch-case statement).

Problem 7
Write a program that inputs three numbers a, b, and c. The program then performs the following requirements:

• Print the largest and the smallest numbers.

• Print these three numbers in ascending order.

Problem 8
According to the Pythagorean theorem, in a right triangle, the square of the hypotenuse is equal to the sum of
the squares of the other two sides. Write a program that inputs three arbitrary numbers and checks whether these
three numbers can be the lengths of the sides of a right triangle.

Problem 9
Write a program that inputs three numbers a, b, and c. The program then performs the following requirements:

• Check if these three numbers are the three sides of a triangle.

• If yes, print the kind of that triangle (scalene, isosceles, right, equilateral).

Problem 10
Write a program that inputs a distance in kilometers. The program then calculates and prints the taxi fare based
on the distance. Given the following rate:

• The first kilometer costs $2.

• From the 2nd to the 5th kilometer, the cost is $1.5 per kilometer.

• From the 6th kilometer onwards, the cost is $1 per kilometer.

• If the distance is more than 120 kilometers, there will be a 10% discount on the total fare.

Problem 11
Write a program that inputs the new and old electric meter readings. The program then calculates and prints the
old reading, new reading, and the total amount due. Give the following rates:

• The first 100 kWh is charged at 1000 VND.

• From 101 kWh to 150 kWh, the rate is 1200 VND per kWh.

• From 151 kWh to 200 kWh, the rate is 2000 VND per kWh.

• From 201 kWh onwards, the rate is 2500 VND per kWh.

2
Problem 12
Write a program that inputs the car’s speed in kilometers per hour over the 60 km/h speed limit (x), and then
calculates and prints the fine for the car. Given the following fine table:

# Kilometers over speed limit Fine


1 5 < x ≤ 10 $28.45
2 10 < x ≤ 20 $101.61
3 20 < x ≤ 35 $223.53
4 35 < x $304.82

Problem 13
Write a program to input an integer x from 0 to 35 and output according to the following rules:

• If x ≤ 9, output x.

• Output "A" if x = 10, "B" if x = 11, "C" if x = 12, ..., "Z" if x = 35.

(Hint: Use static_cast<char> () for x ≥ 10)

Problem 14
Write a program to input three numbers a, b, and c and to solve the quadratic equation ax2 + bx + c = 0.

Problem 15
Write a program to simulate a simple calculator. The program should allow users to input two operands and one
operator (the operators can be: + - * / %). The program then calculates and prints the result of the operation.
For example,

• Input: Operand 1: 2, operand 2: 4, operator: +

• Output: "2 + 4 = 6"

Problem 16
A bank in town A updates its customers’ accounts at the end of each month. The bank offers two types of accounts:
savings and checking. Every customer must maintain a minimum balance. If a customer’s balance falls below the
minimum balance, there is a service charge of $10.00 for savings accounts and $25.00 for checking accounts. If the
balance at the end of the month is at least the minimum balance, the account receives interest as follows:

1. Savings accounts receive 4% interest.

2. Checking accounts with balances of up to $5,000 more than the minimum balance receive 3% interest; other-
wise, the interest is 5%.

Write a program that reads a customer’s account number (int type), account type (char; s for savings, c for
checking), minimum balance that the account should maintain, and current balance. Determine service charges
or interest earned based on balance and account type. The program should then output account details and an
appropriate message.

3
Problem 17
One way to determine how healthy a person is by measuring the body fat of the person. The formulas to determine
the body fat for female and male are as follows:

• Body fat formula for women:

– A1 = (body weight × 0.732) + 8.987


– A2 = wrist measurement (at fullest point) / 3.140
– A3 = waist measurement (at navel) × 0.157
– A4 = hip measurement (at fullest point) × 0.249
– A5 = forearm measurement (at fullest point) × 0.434
– B = A1 + A2 – A3 – A4 + A5
– Body fat = body weight – B
– Body fat percentage = body fat × 100 / body weight

• Body fat formula for men:

– A1 = (body weight × 1.082) + 94.42


– A2 = wrist measurement × 4.15
– B = A1 - A2
– Body fat = wrist measurement - B
– Body fat percentage = body fat × 100 / body weight

• The weight is measured in pounds, and the other measurements are in inches.

Write a program to calculate the body fat of a person.

Problem 18
Obesity causes significant increases in illnesses such as diabetes and heart disease. To determine whether a person
is overweight or obese, you can use a measure called the body mass index (BMI).
Write a program to calculate the BMI of a person. The program should prompt the user to enter his/her weight (W ,
in kilograms) and height (H in meters). The program will use the following formula to perform this calculation:

W
BM I(kg/m2 ) =
H2
The program then prints to the console/screen the following information from the Department of Health and
Human Services/National Institutes of Health so the user can evaluate his/her BMI:

• Underweight: BMI < 18.5

• Normal: 18.5 ≤ BMI < 25

• Overweight: 25 ≤ BMI < 30

• Obesity: BMI ≥ 30

4
Part 2 – Loop Statements

Problem 1
Write a program that inputs a number n, then calculates and prints the sum of all numbers less than n that are
divisible by 4 but not divisible by 5.

Problem 2
2 3 4 n
Write a program that inputs a number n, then calculates and prints the result of S = 1 + + + + ... + .
1 2 3 n−1

Problem 3
Write a program that inputs a number n, then calculates and prints the result of S = 1! + 2! + 3! + ..... + n!.

Problem 4
Write a program that performs the following requirements:

• List all divisors of a positive integer n.

• Determine the number of divisors and find the sum of all these divisors.

Problem 5
Write a program to input a number and check if the number is a prime number (a prime number is a whole number
greater than 1 with only two factors – itself and 1).
Modify the above program to allow users to input a number n (0 < n < 50), and find the largest prime number
less than n.

Problem 6
Write a program that inputs two numbers a and b. The program then performs the following requirements:

• Find the greatest common divisor (GCD) of two numbers a and b (using Euclid’s algorithm).

• Find two integers x and y such that: GCD(a, b) = ax + by. (This is known as Bézout’s identity.)

• Find the least common multiple (LCM) of two numbers a and b.

Problem 7
Write a program to input a positive integer n (up to 4 digits) and print its binary representation.

Problem 8
Write a program to print the multiplication table (from 1 to 9).

5
Problem 9
Write a program to input a number h and print an isosceles triangle using the * character, with the height of h.
For example, with h = 4:

*
***
*****
*******

Problem 10
For each minute of running on a treadmill, a person burns 3.6 calories. Write a program that displays the number
of calories burned after 5, 10, 15, 20, 25, and 30 minutes.

Problem 11
Assume a person’s starting salary is x dollars, and their salary increases by K% each day. Write a program that
inputs the number of working days n (n ≥ 1), the starting salary, and the percentage increase K. The program
then displays the salary for each working day and the total salary received after n days.

Problem 12
Write a program to solve the following two ancient Vietnamese mathematical problems:

1. “Vừa gà vừa chó, bó lại cho tròn, ba mươi sáu con, một trăm chân chẵn (Chickens and dogs, tied together in
a round, thirty-six in all, a hundred feet even)”. What is the number of chickens and dogs?

2. “Trăm trâu trăm cỏ, trâu đứng ăn năm, trâu nằm ăn ba, lụ khụ trâu già, ba con một bó (A hundred buffalo, a
hundred bundles of grass, standing ones eat five, lying ones eat three, old, feeble buffalo, three in a bundle)”.
What is the number of standing cows, old cows, and lying cows?

Problem 13
The population of town A is less than the population of town B. However, the population of town A is growing
faster than the population of town B. Write a program that prompts the user to enter the population and growth
rate of each town. The program then prints the number of years after which the population of town A will be
greater than or equal to the population of town B and the populations of both towns at that time.
For example,

• Population of town A = 4000, growth rate of town A = 40%.

• Population of town B = 5000, growth rate of town B = 20%.

• After two years, the population of town A will be greater than the population of town B (after one year, the
population of town A = 5600, town B = 6000; after two years, town A = 7840, town B = 7200).

6
Problem 14
Write a program to input a number n (n > 2), then calculate and print Fn of the Fibonacci sequence.

F0 = 0
F1 = 1 (1)
Fn = Fn−1 + Fn−2

Problem 15
Assume a class list where students are arranged in ascending order of their heights. The shortest number is number
1. Write a program to help the teacher find the heights of the first and last students in the class list. The program
allows the teacher to input a sequence of arbitrary heights and stops when the teacher enters 0. The program
displays the results. Heights are measured in meters.
For example,

Enter height: 1.52


Enter height: 1.4
Enter height: 1.25
Enter height: 1.53
Enter height: 0

The height of the first student in the class list: 1.25 (m)
The height of the last student in the class list: 1.53 (m)

Problem 16
Write a program to simulate a simple calculator. The program should display the following calculation menu:

SIMPLE CALCULATOR
1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit program

Your choice:

If the user enters 1, the program will prompt the user to input two numbers and calculate their sum. Similarly, for
2, subtraction is performed, for 3, multiplication, and for 4, division. The program will repeat until the user enters
0 (to exit the program).

7
For example,

SIMPLE CALCULATOR
1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit program

Your choice: 3

Enter the first operand: 2


Enter the second operand: 3
Result: 2 * 3 = 6
----------------------------------------
SIMPLE CALCULATOR
1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit program

Your choice: 0

You exited the program!

Problem 17
Write a program to simulate a countdown timer. The program allows users to input the number of minutes and
seconds, and then displays as follows:

Enter the number of minutes: 0


Enter the number of seconds: 10

00:10
00:09
00:08
00:07
00:06
00:05 Tick-tock
00:04 Tick-tock
00:03 Tick-tock
00:02 Tick-tock
00:01 Tick-tock
00:00 Ding-dong

8
Problem 18
Assume the first number of a sequence is x, where x is an integer. The sequence is defined as follows:

 a =x
 0


an+1 = a2n , if an is even (2)



n+1 = 3 × an + 1, if an is odd
a

It’s known that there exists an integer k such that ak = 1.


Write a program that inputs the initial value x, calculates the sequence until a term ak = 1, then prints the entire
sequence and the value of k.
For example,

• Input: x = 75

• Output:

– The sequence: 75, 226, 113, 340, 170, 85, 256, 128, 64, 32, 16, 8, 4, 2, 1
– k = 14

Test your program with the following values of x: 75, 111, 678, 732, 873, 2048, and 65535.

Problem 19
Write a program to print the ASCII table. The table should have two columns: one for the characters (all 26
uppercase and lowercase English letters) and the other for their corresponding ASCII codes.

Problem 20
Write a program to input a positive number n and perform the following requirements:

• Print the reverse of n. For example, n = 123 → the reverse is 321.

• Count the number of odd and even digits in n.

You might also like