Computer Programming - I (MCT-143) Lab Session 5 If and If-Else Conditions
Computer Programming - I (MCT-143) Lab Session 5 If and If-Else Conditions
Computer Programming - I (MCT-143) Lab Session 5 If and If-Else Conditions
Lab Session 5
if and if-else Conditions
See relational operators at page 77 of the text book. Detail of if statement is at page 94 of the text book.
See program of if statement given at page 94 and 96. Detail of if-else is at page 98.
Similarly relational operators are given at page 158 of the reference book. If statement syntax is given at
page 164 fig. 4-3 of the reference book. Detail of if-else is at page 172.
Task 1:
Write a program that will take a number from user and will display at the output whether it is an even
number or odd. Use two if conditions for it.
Task 2:
Repeat above task using if-else statement.
More than one condition can also be used in one if or if-else statement using logical operators which are
given at page 116 of the text book and at page 187 of the reference book.
For example if you want to check that value of variable a is greater than 5 and less than 10, it will be done
like:
if (a>5 && a<10)
Task 3:
Write a program that will ask for the marks of five subjects one by one and will print the average of the
marks. Moreover a message will be printed out depending upon average marks as:
If average is greater than or equal to 80 You are outstanding student.
If average is greater than or equal to 70 but less than 80 You are good student.
If average is greater than or equal to 60 but less than 70 You are an average student.
If average is greater than or equal to 50 but less than 60 You are below average student.
If average is greater than or equal to 40 but less than 50 You are poor student.
If average is less than 40 You need extra ordinary efforts
Max marks are 100 and you can assume that marks are of integer data type.
Task 5:
Write a program that will take three integers as input and will print the maximum of three numbers at
output. You have to use three if statements.
Task 6:
Write a program that will take two numbers as input (say a and b) and will show their ratio at output (a/b).
As you know that division by zero is not possible and a run-time error occurs in an attempt to divide by
zero. You have to incorporate this problem in a way that if second number entered is zero, program should
not calculate the ratio; instead it should display a message that division by zero is not possible.
Task 8:
Write a program to find the square root of a number. You have to take care of complex results as well.
Sample output is:
Enter a number: 27
Its square root is: 5.196