Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Computer Programming - I (MCT-143) Lab Session 5 If and If-Else Conditions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Computer Programming – I (MCT-143)

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.

Sample output is:


Enter subject 1 marks: 74
Enter subject 2 marks: 68
Enter subject 3 marks: 82
Enter subject 4 marks: 65
Enter subject 5 marks: 87
Average of five subject marks is: 75.2
You are a good student
Task 4:
In last lab session last task was to take input three sides of triangle and show the area of triangle. As you
know that for a valid triangle the condition is that sum of any two sides should be greater than the third
one. So update that program such that it takes three sides as input and will print the area only if it is a valid
triangle otherwise it should print that triangle is invalid.

Sample output is:


Enter 1st side of triangle: 4
Enter 2nd side of triangle: 3
Enter 3rd side of the triangle: 5
The area of triangle is: 6

Another sample output is:


Enter 1st side of triangle: 6
Enter 2nd side of triangle: 3
Enter 3rd side of the triangle: 2
It is not a valid triangle...

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.

Sample output is:


Enter first integer: 12
Enter second integer: 16
Enter third integer: 5

Second number 16 is the largest

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.

Sample output is:


Enter first number: 5
Enter second number: 2
Ratio of two numbers is: 2.5

Another sample output is:


Enter first number: 16
Enter second number: 0
Division by zero is not possible!
Task 7:
Write a program that will take a number from user and will display at output whether it is a even number
or the odd and also displays its magnitude.

Sample output is:


Enter a number: 46
It is an even number with magnitude 46.

Another sample output is:


Enter a number: -147
It is an odd number with magnitude 147.

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

Another sample output is:


Enter a number: -44
Its square root is: 6.633i

You might also like