0% found this document useful (0 votes)
8 views35 pages

Week 4 Lab

This document outlines a lab session for CS101 focusing on conditional selection statements in C++. It covers various operators, selection statements, and provides multiple programming problems to practice these concepts in C++. The problems range from basic arithmetic operations to more complex decision-making scenarios using if-else and switch statements.

Uploaded by

zakhro06
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)
8 views35 pages

Week 4 Lab

This document outlines a lab session for CS101 focusing on conditional selection statements in C++. It covers various operators, selection statements, and provides multiple programming problems to practice these concepts in C++. The problems range from basic arithmetic operations to more complex decision-making scenarios using if-else and switch statements.

Uploaded by

zakhro06
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

CS101 Introduction to Programming Lab session - # 4

Term: Spring 2026

Topic: Conditional selection statement in C++

Content

• Operators
– Arithmetic Operators: +, -, *, /, %
– Assignment Operators: =, +=, -=, *=, /=, %=
– Comparison Operators: >=, <=, ==, !=
– Logical Operators: &&, ||, !
• Selection Statements
– if Statement
– if-else Statement
– if-else if-else Statement
– switch Statement
Objectives

In this lab, you will:


• Gain a clear understanding of different types of operators in C++, including:
– Arithmetic operators (+, -, *, /, %) for performing mathematical op-
erations.
– Assignment operators (=, +=, -=, *=, /=, %=) for assigning and up-
dating values.
– Comparison operators (>=, <=, ==, !=) for evaluating conditions.
– Logical operators (&&, ||, !) for combining multiple conditions.
• Learn how to use **selection statements** to control program flow:
– Understand the if statement and when to use it.
– Use if-else statements for handling multiple conditions.
– Implement if-else if-else for complex decision-making.
– Work with the switch statement for handling multiple cases efficiently.
• Develop skills to write structured and efficient C++ programs that make
decisions based on user input and conditions.

Page 2
Problem 1
You are given seven real numbers:
a, b, c, d, e, f, g
Compute the value of the expression:
a+b e
+
c−d f +g
Example:
Input: 2 4 10 5 6 3 1
Output: 2.7

Page 3
Problem 2

You are given three real numbers:


x1, x2, x3
Compute the population variance using the formula:
x1 + x 2 + x 3
µ=
3
(x1 − µ)2 + (x2 − µ)2 + (x3 − µ)2
Var =
3
Example:
Input: 2 4 6
Output: 2.667

Page 4
Problem 3
Determine if a number is positive or negative.
1. Take a number as input.
2. Use an if/else statement to check whether it is positive or
negative.
3. Print the result.
Example:
Input: -5
Output: Negative number

Page 5
Problem 4
Check if a number is even or odd using the modulus
operator.
1. Take a number from the user.
2. Use the modulus operator (%) to check divisibility by 2.
3. Print whether the number is even or odd.
Example:
Input: 7
Output: Odd number

Page 6
Problem 5

Find the largest of two numbers.


1. Take two numbers as input.
2. Use an if/else statement to compare them.
3. Print the largest number.
Example:
Input: 12, 25
Output: Largest number is 25

Page 7
Problem 6
(Modify problem 3) Find the largest among three
numbers.
1. Take three numbers as input.
2. Use nested if/else statements to determine the largest.
3. Print the largest number.
Example:
Input: 10, 20, 15
Output: Largest number is 20

Page 8
Problem 7

Write a program that prompts the user to enter the speed


of a vehicle. If speed is less than 20, display too slow; if speed
is greater than 80, display too fast; otherwise, display just
right.
Example:
Input: 25 Input: 120
Output: just right Output: too fast

Page 9
Problem 8
Determine pass or fail based on marks.
1. Take marks as input.
2. If marks are 50 or above, print ”Pass”, else print ”Fail”.
Example:
Input: 45
Output: Fail

Page 10
Problem 9
Categorize a number as positive, negative, or zero
using nested if/else.
1. Take a number as input.
2. Use nested if/else to categorize the number.
3. Print the category.
Example:
Input: 0
Output: Zero

Page 11
Problem 10

Write a program that prompts the user to enter two inte-


gers and checks whether the first number is divisible by the
second.
Example:
Input: 2 3 Input: 10 5
Output: 2 is not divisible by Output: 10 is divisible by 5
3

Page 12
Problem 11

Write a program that prints commands for traffic lights.


• if input is ’g’, output is ”Go!”.
• if input is ’y’, output is ”Get ready!”.
• if input is ’r’, output is ”Stop”.

Page 13
Problem 12
Write a program (using switch statement)that input a student’s
grade (0-100) in integer from the keyboard and prints the cor-
responding scale (e.g. A, B, C, D, F)according to the following
rule.

•90-100 A
•80-89 B
•70-79 C
•60-69 D
•0-59 F
Example:
Input: 63 Input: 23
Output: D Output: F

Page 14
Problem 13

Write a C++ program to check whether a triangle can be


formed by the given value for the angles.
Example:
Input: 50 40 70 Input: 20 45 120
Output: The triangle is valid Output: The triangle is not
valid

Page 15
Problem 14
A university offers a scholarship based on a student’s academic
performance, attendance, and project work.
There are 14 lectures in total. You are given the number of
absences, and you must compute the attendance percentage:
14 − absences
attendance% = × 100
14
A student is considered ELIGIBLE if at least one of the
following conditions is satisfied:
1. CGPA is at least 3.5 AND attendance is at least 90% AND
completed at least 3 projects.
2. CGPA is at least 3.7 AND attendance is at least 80% AND
completed at least 2 projects.
3. CGPA is at least 4.0 AND attendance is at least 70% AND
completed at least 1 project.

Page 16
Otherwise, the student is NOT ELIGIBLE.
Example:
Input: Input:
GPA:3.9 GPA:4.0
Absences:5 Absences:1
Projects:3 Projects:2
Output: Not eligible Output: Eligible

Page 17
Problem 15
A customer may receive a free internet speed upgrade.
First, calculate the average download speed:
speed1 + speed2 + speed3
avgSpeed =
3
The upgrade is granted if:
• avgSpeed ≥ 100 Mbps AND
• (monthlyPayment ≥ 50 OR loyaltyYears ≥ 3)
However, the upgrade must NOT be granted if:
• outstandingBalance > 0.

Input: speed1, speed2, speed3, monthlyPayment, loyal-


tyYears, outstandingBalance
Output

Page 18
Print:
UPGRADE
or
NO UPGRADE
Example:
Input: 110 95 105 60 1 0 Input: 130 120 115 80 5 20
Output: UPGRADE Output: NO UPGRADE

Page 19
Problem 16
A smart car decides whether the engine can start automatically.
The engine is allowed to start if at least one of the following
logical conditions is satisfied:
1. The key is detected AND the brake is pressed.
2. The remote start is activated AND the temperature is below
5°C.
3. The emergency override is activated.
However, the engine must NOT start if:
• The fuel level is below 10%.
Input:
Five values: keyDetected, brakePressed, remoteStart, tem-
perature emergencyOverride, fuelLevel

Page 20
Where:
• keyDetected (0 or 1)
• brakePressed (0 or 1)
• remoteStart (0 or 1)
• temperature (integer, °C)
• emergencyOverride (0 or 1)
• fuelLevel (integer percentage 0–100)
Output
Print:
ENGINE STARTS
or
ENGINE LOCKED

Page 21
Problem 17

Write a C++ program to calculate the root of a Quadratic


Equation:
a ∗ x2 + b ∗ x + c = 0,
where a,b,c are non-integer input.
Example:
Input: 6 0.5 -4 Input: 2 4 2
Output: Output:
x1=0.775892 x1=-1
x2=-0.859226 x2=-1

Page 22
Problem 18

Write a program to check whether a entered character is


lowercase ( a to z ) or uppercase ( A to Z ). In case it is not
alphabet write that it is not alphabet.
Example:
Input: V Input: !
Output: Uppercase alphabet Output: It is not an alphabet

Page 23
Problem 19

(Financial: compare costs) Suppose you shop for two differ-


ent packages of rice. You would like to write a program to
compare the cost. The program prompts the user to enter the
weight and price of each package and displays the one with the
better price.
Example:
Input: Input:
Enter weight and price for pack- Enter weight and price for pack-
age 1: 50 24.59 age 1: 50 25
Enter weight and price for pack- Enter weight and price for pack-
age 2: 25 11.99 age 2: 25 12.5
Output: Package 2 has a bet- Output: Two packages have
ter price the same price.

Page 24
Problem 20

(Palindrome number) Write a program that prompts the user


to enter a three-digit integer and determines whether it is a
palindrome number. A number is palindrome if it reads the
same from right to left and from left to right.
Example:
Input: Enter a three-digit in- Input: Enter a three-digit
teger: 121 integer: 123
Output: 121 is a palindrome Output: 123 is not a palin-
drome

Page 25
Problem 21

Write a C++ program that knows how to greet in 4 lan-


guages and allows user to choose the language computer greets
them:
• u - Uzbek
• e - English
• r - Russian
• g - German
Example:
Input: g Input: k
Output: I do not know this
Output: Hallo language:(

Page 26
Problem 22

Write a program that prompts the user to enter a point (x,


y) and checks whether the point is within the circle centered at
(0, 0) with radius 10. For example, (4, 5) is inside the circle and
(9, 9) is outside the circle, as shown in figure.
Hint: A point is in the circle if its distance to (0, 0) is less
than
p or equal to 10. Formula for computing the distance is
(x2 − x1)2 + (y2 − y1)2 .

Example:

Page 27
Input: 4 5 Input: 9 9
Output: Output:
Point (4,5) is in circle Point (9,9) is not in circle

Page 28
Problem 23
Write a program (using switch statement)that input a student’s
GPA (0-4.5) from the keyboard and prints the corresponding
scholarship amount according to the following rule.

•4.0-4.5 80%
•3.5-4.0 60%
•3.0-3.5 50%
•smaller 3.0 no scholarship
Example:
Input: 4.1 Input: 3.3
Output: You got 80% schol- Output: You got 50% schol-
arship arshi

Page 29
Problem 24
(Find future dates) Write a program that prompts the user to
enter an integer for today’s day of the week (Sunday is 0, Monday
is 1, . . . , and Saturday is 6). Also prompt the user to enter
the number of days after today for a future day and display the
future day of the week.
Example:
Input: Input:
Enter today’s day: 1 Enter today’s day: 0
Enter the number of days Enter the number of days
elapsed since today: 3 elapsed since today: 3
Output: Today is Monday Output: Today is Sunday
and the future day is Thursday and the future day is Wednesday

Page 30
Problem 25
You are given an integer representing a year. Determine whether
this year is a leap year.
A year is a leap year if:
• It is divisible by 400, OR
• It is divisible by 4 and NOT divisible by 100.
Otherwise, it is not a leap year.
Example:
Input: 2024
Output: Leap year

Page 31
Problem 26
(Cost of shipping) A shipping company uses the following
function to calculate the cost (in som) of shipping based on the
weight of the package (in kg).



3500, if 0 < w <=1

5500, if 1 < w <=3
c(w) =


8500, if 3 < w <=10

10500, if 10 < w <=20

Write two programs, one using if/else and the other switch/-
case, that prompt the user to enter the weight of the package
and display the shipping cost. If the weight is negative or zero,
display a message “Invalid input.” If the weight is greater than
20, display a message “The package cannot be shipped.”

Page 32
Example:
Input: 2 Input: 25
Output: The package cannot
Output: 5500 be shipped.

Page 33
Problem 27
You are given a time in 24-hour format:
hh mm
Create its mirror time using the following rule:
• The mirrored hour is obtained by reversing the digits of the
minutes.
• The mirrored minutes are obtained by reversing the digits of
the hour.
For example:
12 34 → 43 21
Your task:
1. Print the mirrored time.
2. Print YES if the mirrored time is a valid 24-hour time, other-
wise print NO.

Page 34
Example:
Input: 12 34 Input: 21:02
Output: 43:21 Output: 20:12
No Yes

Page 35

You might also like