Lab No. 2: Control Structures: Objective
Lab No. 2: Control Structures: Objective
Scope:
Sometimes we need to make a decision between two choices. The statements in by which we make this
decision are called conditional statements. We use loops to make one or more statements repeat for speci-
fied number of times. Clearly this has an importance when we wish to take more than one input from user
or do some calculations.
Example 4: Following program that keeps reading numbers (integers) from user until input is 0, then prints
average, sum, maximum and minimum.
Exercises
Exercise 1:
Write a program that specifies whether a given number (x) falls in one of the following categories (give x a
value from the code, don't read from user):
• 0 to 9
• 10 to 19
• 20 to 29
• None of the categories
For example, if x = 5, program should print "0 to 10", and if x = 44 it should print "None". Hint:
use if statements with Boolean expressions combined using &&.
Exercise 2:
Write a program that reads two integers and prints their sum like the code below (text shown in boldface is
supposed to be user input). Enter the first number: 3
Enter the second number: 4
The sum is 7
Exercise 3:
Write a program that asks the user to enter two numerical values (integers) and then select an operation
(addition, subtraction, multiplication and division) then prints the result based on operation selected. The
code below shows examples of the output (text shown in boldface is supposed to be user input). Enter first
number: 4 Enter second number: 2 1. Addition (+).
2. Subtraction (-).
3. Multiplication (*).
4. Division (/).
Enter operation number: 3
The result is 8
Exercise 4:
Write a program that reads 10 numbers from the user then prints out how many positive numbers andnegative
numbers user has entered (consider 0 a positive number).