CSE 103 Lab-3
CSE 103 Lab-3
Lab Manual
Course : CSE 105
Credit Title : Structured Programming
Instructor : Dr. Maheen Islam, Associate Professor, CSE Department
Lab-3: Selection Statements if, if-else, if-else-if, Switch and Conditional operators
As discussed in the class, selection structures are used to choose among alternative courses of
action: In this problem we want to check whether a student is passes or failed. According to
the rule of EWU If the marks of a student is greater than or equal to 60 he/she “Passed”.
Thus, the flowcharts of this problem will be as follows:
if(grade>=60)
printf( "Passed\n" );
i. Fill the printf and scanf lines such that the output is as follows:
Page 1 of 5
ii. As you see there is no output for the input 59 .This is because if statement is a single-
entry/single-exit structure, i.e. our code will perform action for the true condition only.
iii. Using an else concatenate with if we can easily covert the above code such it can perform an
action (for example print failed) for the false condition too. In that case, it flow chart will be
as follows
iv. In the above code replace the if parts with the following code.
if(expression)
Statement
Page 2 of 5
Statement_1;
Statement_2;
… … …
Statement_n;
}
Placing braces around a group of statements forces the compiler to treat it as a single
statement, i.e., for the following C code, all n statements will be executed if the expression is
true.
if(expression)
{
Statement_1;
Statement_2;
… … …
Statement_n;
}
Exercise 2: Consider the following flowchart, and Modify your code of exercise 1, such that it
can comply outputs shown in right
Outputs
Note: For this you should use separate printf for each text i.e., one printf for Sorry, one for
Failed, one for Congratulations and another one for Passed .
Exercise 3: You are given two integer numbers. Find the quotient and remainder. When 2nd
input is zero, you should print “Error!! Can’t divide.”
Page 3 of 5
Exercise 4: In this exercise you need to write a program that will take an integer input
representing year and print whether this is a leap year or not leap year.
Exercise 5: Write a C code that can check whether a number is odd or even. Your number
should be input from the keyboard. If the number is odd your program should print “The number
you entered is ODD” otherwise it should print “The number you entered is EVEN”. A number is
odd if the number mod 2 equals to zero, otherwise it is an even number.
Exercise 6: Given a score and the following grading scale write a program with if-else-if only to
find the corresponding grade.
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
Your program should have proper error checking. For example, if a user input a negative number
or more than 100 it should print “Invalid input.”
Exercise 7: Suppose we have two tasks A and B, A takes Ah hours, Am minutes, and As
seconds. On the other hand B takes Bh hours, Bm minutes, and Bs seconds. Write if-else
statements to print out which task takes more time?
Page 4 of 5
Please Enter As = 13
Please Enter Bh = 8
Please Enter Bm = 35
Please Enter Bs = 37
Exercise 8: Write a program that reads 3 integer numbers a, b and c from user and computes
minimum, median and maximum of the numbers
Page 5 of 5