0% found this document useful (0 votes)
72 views5 pages

CSE 103 Lab-3

Uploaded by

amivalo45rt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views5 pages

CSE 103 Lab-3

Uploaded by

amivalo45rt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

East West University

Department of Computer Science & Engineering


________________________________________________________________________________________________________

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:

In C we can do this using a single if, which is shown below.

if(grade>=60)
printf( "Passed\n" );

Exercise 1: Consider the following code

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.

v. What is the output for the input 70 and 59 here?

In the if statement template, notice that statement is singular, not plural:

if(expression)
Statement

To make an if statement control two or more statements, generally we use a compound


statement. A compound statement has the form
{

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.”

Sample Input Sample Output


17 5 Q=3, R=2
3 0 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.

Sample Input Sample Output


2000 Leap Year
2003 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.

Sample input Sample Output


Please Enter your number : 11 The number you entered is ODD
Please Enter your number : 120 The number you entered is EVEN

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.”

Sample input Sample Output


Please Enter the number : 97 Grade is A.
Please Enter the number : 75 Grade is C.
Please Enter the number : 107 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?

Sample input Sample Output


Please Enter Ah = 10 B takes more time.
Please Enter Am = 13
Please Enter As = 35
Please Enter Bh = 11
Please Enter Bm = 0
Please Enter Bs = 0
Please Enter Ah = 11 A takes more time.
Please Enter Am = 12

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

Sample input Sample Output


Please Enter a = 2 Min = 2, Max = 5, Median = 3
Please Enter b = 5
Please Enter c = 3
Please Enter a = 2 Min = 2, Max = 3, Median = 2
Please Enter b = 2
Please Enter c = 3

Exercise 9: Write a program that reads a point


(x, y) from user and prints its region

Sample input Sample Output


Please Enter x, y: 3 -1 This point is in Region 4
Please Enter x, y: -1 -5 This point is in Region 3

Page 5 of 5

You might also like