0% found this document useful (0 votes)
18 views

Lab 2

Bài 2 - Thực hành Cơ sở lập trình trường Đại học Khoa Học Tự Nhiên
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab 2

Bài 2 - Thực hành Cơ sở lập trình trường Đại học Khoa Học Tự Nhiên
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 2.

Control structures Fundamentals of Programming CSC10012

Lab 2

Control structures
In this lab, we will learn about control structures which include conditional and loop statements.

Instruction
1. Conditional Statements
Conditional statements allow the program to make decisions based on conditions (selectively).
Below are some common conditional statements in C/C++.

• if statement (one-way selection): Executes a block of code if the condition is true.


1 if ( condition )
2 {
3 // Code
4 }

• if-else statement (two-way selection): Executes a block of code if the condition is true,
otherwise executes another block of code.
1 if ( condition )
2 {
3 // Code
4 }
5 else
6 {
7 // Code
8 }

• else-if statements (multiple-way selection): Allows checking multiple conditions.


1 if ( condition )
2 {
3 // Code
4 }
5 else if ( condition )
6 {
7 // Code
8 }

University of Science Faculty of Information Technology Page 1


Lab 2. Control structures Fundamentals of Programming CSC10012

• switch statement: An alternative to if-else when there are multiple conditions based on
the value of a single variable.
1 switch ( variable )
2 {
3 case < first value >:
4 // statements here
5 break ; // Prevents fall - through to subsequent cases
6 case < second value >:
7 // statements here
8 break ;
9 default :
10 // statements here
11 }

2. Loop Statements
Loop statements allow executing a block of code repeatedly. C/C++ provides three types of loops:

• for loop: Used when the number of iterations is known.


1 for ( int i = 1; i <= 10; i ++)
2 {
3 cout << " Number : " << i << endl ;
4 }

• while loop: Repeats a block of code while a condition remains true.


1 int i = 1;
2 while ( i <= 10)
3 {
4 cout << " Number : " << i << endl ;
5 i ++;
6 }

• do-while loop: Executes the code at least once and then continues if the condition is true.
1 int i = 1;
2 do
3 {
4 cout << " Number : " << i << endl ;
5 i ++;
6 } while ( i <= 10) ;

University of Science Faculty of Information Technology Page 2


Lab 2. Control structures Fundamentals of Programming CSC10012

Exercises
Exercise 1. Letter grade scale
Write a program to input a score on the ten-point scale and convert it to the letter grade scale.
The conversion method is defined in the table below.
Ten-point scale Letter grade scale
8.0 ≤ x ≤ 10 A
6.0 ≤ x < 8.0 B
5.0 ≤ x < 6.0 C
3.0 ≤ x < 5.0 D
x < 3.0 F

Example:

Input Output
9.2 A

Exercise 2. Number to Word Conversion


Write a program to input an integer from 0 to 10 and print its English word. If the input number
is not between 0 and 10, print Unknown.
Example:

Input Output
7 Seven
-10 Unknown

Exercise 3. Solve equation


Write a program to input three coefficients a, b, c and solve the equation ax2 + bx + c = 0.
Example:
Input Output
1 -3 2 2 1
1 2 1 -1
0 2 -1 0.5
1 1 1 No solutions

University of Science Faculty of Information Technology Page 3


Lab 2. Control structures Fundamentals of Programming CSC10012

Exercise 4: Triangle Classification


Write a program to input three sides of a triangle and determine its type:

• Not a triangle: If the sum of any two sides is not greater than the third side.

• Equilateral triangle: If all three sides are equal.

• Isosceles triangle: If two sides are equal.

• Right triangle: If it satisfies the Pythagorean theorem a2 + b2 = c2 .

• Right isosceles triangle: If it is a right triangle with two equal sides.

• Regular triangle: If it does not fit any of the above categories.

Example:

Input Output
3 3 3 Equilateral triangle

Exercise 5: Tomorrow
Write a program to input three values: day, month, and year. The program should determine and
output tomorrow’s date. There are some information:

• January, March, May, July, August, October, December: 31 days.

• April, June, September, November: 30 days.

• February: 29 days in a leap year, otherwise 28 days.


A leap year is divisible by 4 but not divisible by 100, unless it is also divisible by 400.

Example:

Input Output
31 12 2023 1 1 2024
28 2 2024 29 2 2024
28 2 2023 1 3 2023
29 2 2023 Not a valid date

University of Science Faculty of Information Technology Page 4


Lab 2. Control structures Fundamentals of Programming CSC10012

Exercise 6. Approximate ex
Write a program to approximate the value of ex using the Maclaurin expansion up to k terms.
The formula for ex is:

x x2 x3 xk
ex = 1 + + + + ··· +
1! 2! 3! k!
Your program should input two values: a float x and an integer k (the number of terms).
Example:

Input Output
1 5 2.71667
0.5 10 1.64872
-1 15 0.367879

Exercise 7. Check for prime number


Write a program to input an integer n and check if it is a prime number.
Hint: A prime number is a natural number greater than 1 that is divisible only by 1 and itself.
Example:

Input Output
2 Prime
3 Prime
4 Not Prime

Exercise 8. Check for perfect number


Write a program to input an integer n and check if it is a perfect number.
Hint: A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding
the number itself.
Example:

Input Output
6 Perfect
28 Perfect
12 Not Perfect
15 Not Perfect

University of Science Faculty of Information Technology Page 5


Lab 2. Control structures Fundamentals of Programming CSC10012

Exercise 9. Reverse number


Write a program to input an integer n (the first and last digits are not zero), then reverse its digits.
If n ends with zero, print No reverse number.
Example:
Input Output
12345 54321
12340 No reverse number

Exercise 10. Find the smallest and biggest digit of an integer


Write a program to input an integer n and find its smallest and biggest digits.
Example:
Input Output
34567 3 7
9876543210 0 9
9999 9 9

Submission
Your source code must be contributed in the form of a compressed file and named your submission
according to the format StudentID.zip. Here is a detail of the directory organization:
StudentID
Exercise 1.cpp
Exercise 2.cpp
Exercise 3.cpp
Exercise 4.cpp
Exercise 5.cpp
Exercise 6.cpp
Exercise 7.cpp
Exercise 8.cpp
Exercise 9.cpp
Exercise 10.cpp

The end.

University of Science Faculty of Information Technology Page 6

You might also like