You are on page 1of 6

Lab # 04

DECISIONS

Objectives:
If-else
Switch
Boolean Operator

Arithmetic Expressions:
(a) If (No else branch)
In C++ if statements are used to implement a decision. When the condition is fulfilled the
statements mentioned in the body of if are executed.

1: Input an integer from the user and check if it is divisible by 2.


(b) If-else Statement:
In If-else statements when a condition is fulfilled one sets of statement is executed, else the
other set of statements is processed.

2: Write a program to input two integers and find out whether the first integer is greater or
the second.
C++ has a selection operator of the form

condition? value1: value2

The value of that expression is either value1 if the test passes or value2 if it fails.

3: Re-write the program 2 using the selection operator.


In many cases, the condition involves comparing two values. The comparison is called a
relational operator. C++ has six relational operators.

(c) Multiple Alternatives


In many situations, there are more than two branches involve. In this section, you will see how
to implement a decision with multiple alternatives.

4: Write a program to find out the grade of student based on the average score. The grade is
calculated as
If score is greater than or equal to 90, the grade is A
If score is greater than or equal to 80, the grade is B
If score is greater than or equal to 70, the grade is C
If score is greater than or equal to 60, the grade is D
If score is less than 60, the grade is F
LAB TASK:
1: Write a program to input an integer from the user, check if it is an even number or odd
number and print the message.

2: Write a program to find the roots of a quadratic equation. The roots are calculated as

If discriminant is greater than zero then the roots are real


root1=(-b+disc) / (2*a)
root2=(-b+disc) / (2*a)
If discriminant is less than zero then the roots are imaginary
root1=(-b+idisc) / (2*a)
root2=(-b+idisc) / (2*a)
If discriminant is zero then the roots are real and equal
root1=root2=(-b) / (2*a)

Note: Submit the lab report of these two lab tasks in the next lab.

You might also like