0% found this document useful (0 votes)
22 views6 pages

EEE 105 Lab-3 - If-Else

This lab manual for EEE 105 at East West University covers selection statements in C programming, including if, if-else, and switch statements. It includes exercises for students to implement various programming tasks such as grade evaluation, leap year checking, and basic arithmetic operations. The manual emphasizes the use of flowcharts and proper error handling in coding.

Uploaded by

Sakib Subah
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)
22 views6 pages

EEE 105 Lab-3 - If-Else

This lab manual for EEE 105 at East West University covers selection statements in C programming, including if, if-else, and switch statements. It includes exercises for students to implement various programming tasks such as grade evaluation, leap year checking, and basic arithmetic operations. The manual emphasizes the use of flowcharts and proper error handling in coding.

Uploaded by

Sakib Subah
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/ 6

East West University

Department of Electrical & Electronic Engineering


Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219

Lab Manual
Course ​ ​ : EEE 105
Credit Title​ ​ : Computer Programming

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 6

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 59here?

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 6
​ 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

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 .

​ ​ Page 3 of 6
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.

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: Complete according to grade sheet GRADE SHEET Given a score and the
following grading scale write a program with if-else-if only to find the corresponding grade.

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

Numerical Scores Letter Grade

80-100 A+

75-79 A

70-74 A-

65-69 B+

60-64 B

55-59 B-

​ ​ Page 4 of 6
Numerical Scores Letter Grade

50-54 C+

45-49 C

40-44 D

Less than 40 F

Sample input Sample Output


Please Enter the number : 75 Grade is A.
Please Enter the number : 49 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
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

​ ​ Page 5 of 6
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 6 of 6

You might also like