File 21

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

North South University

Department of Electrical and Computer Engineering


CSE115L: Programming Language I Lab

Lab 7: To be familiar with Decision Statements in C

Example 1: Write a C program that will display a comment based on your marks.

Grade Comment
A Excellent
B Good
C Average
D Below Average
F Failing
Any other input Invalid Grade

Problem Analysis: The input parameter for this problem is identified as grade (character type), and the output
parameter is identified as message. Given the grading system and corresponding comments, the program needs
to determine the appropriate comment based on the grade input by the user. The grading system consists of grades
A, B, C, D, F, and any other input is considered an invalid grade.

To achieve this, the program needs to use conditional statements such as if-else or switch-case to check the value
of the grade input and assign the corresponding comment accordingly. We will use both if-else and switch case.

Input variables Processing Output variables Necessary header


variables/calculations files/functions/macros
grade (char) stdio.h

Algorithm: Code (with if-else):

1. Start the program.


2. Display a prompt for the user: "Enter your
grade (A, B, C, D, or F):".
3. Read the grade input from the user.
4. If the grade is 'A', display "Excellent".
5. If the grade is 'B', display "Good".
6. If the grade is 'C', display "Average".
7. If the grade is 'D', display "Below Average".
8. If the grade is 'F', display "Failing".
9. If the input is not any of the above grades,
display "Invalid Grade".
10. End the program.

1|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

Code (with switch-case):

Without User Defined Function

2|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

User Defined Function

with No Parameter and No return

3|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

User Defined Function

with Parameter but No return

4|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

User Defined Function

with No Parameter but with Return

5|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

User Defined Function

with Parameter and Return

6|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

Output
Enter the grade (A-F): A
Excellent

Discussion and Conclusion: In this problem, we applied our knowledge of conditional statements in C
programming to assign comments based on user input grades. While we previously learned about if-else
statements, today we also explored switch-case. This allowed us to handle different scenarios efficiently and
broaden our understanding of conditionals. Overall, this exercise enhanced our problem-solving skills and
familiarity with conditional logic in C programming.

Example 2: Write a program that acts as a simple calculator. The user inputs two numbers and the operation they
wish to perform (+, -, *, /). Use a switch-case to execute the appropriate operation and print the result. Handle
division carefully to avoid division by zero.

Problem Analysis: The input parameters for this problem are identified as num1 (float type), num2 (float type),
operation (character type), and the output parameter is identified as result (float type).
The program should prompt the user to enter two numbers and the desired arithmetic operation (+, -, *, /).
It should then use a switch-case statement to execute the appropriate operation and print the result. Special
attention needs to be given to division to prevent division by zero errors.
To achieve this, the program must validate the user's input operation and handle division by zero by
checking if the second number is zero before performing division.

Input variables Processing Output variables


Necessary header
variables/calculations files/functions/macros
num1 (float) result (float) stdio.h
num2 (float)
operation (char)

Algorithm:
1. Start the program.
2. Prompt the user to enter the first number (num1).
3. Read and store the value of num1.
4. Prompt the user to enter the second number (num2).
5. Read and store the value of num2.
6. Prompt the user to enter the arithmetic operation they want to perform (+, -, *, /).
7. Read and store the operation.
8. Use a switch-case statement to perform the following actions based on the operation:
a. If the operation is '+', add num1 and num2, and store the result.
b. If the operation is '-', subtract num2 from num1, and store the result.
c. If the operation is '*', multiply num1 and num2, and store the result.
d. If the operation is '/', check if num2 is not equal to zero:
9. If num2 is not zero, divide num1 by num2 and store the result.
10. If num2 is zero, display an error message for division by zero.
11. Display the result of the arithmetic operation.
End the program.
7|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

Without Used Defined Function: using if-else

Without User Defined Fucntion: using swtich-case

8|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

User Defined Function

with No Parameter and No return

9|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

User Defined Function

with Parameter but No return

10 | P a g e
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

User Defined Function

with No Parameter but with Return

11 | P a g e
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

User Defined Function

with Parameter and Return

12 | P a g e
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

Output
Enter the first number: 4
Enter the second number: 5
Enter the operation (+, -, *, /): *
Result: 20.00

Discussion and Conclusion: This problem allowed us to create a basic calculator program in C that performs
arithmetic operations based on user input. By implementing switch-case statements, we were able to execute the
appropriate operation chosen by the user. Additionally, we handled the potential issue of division by zero by
incorporating a conditional statement to check for this condition before performing division. Overall, this
exercise enhanced our understanding of control flow and error handling in C programming.

Exercises:
1. Ask the user to input a number from 1 to 7, where each number corresponds to a day of the week,
starting with 1 for Sunday. Use a switch-case to print the day of the week. If the number is outside the
range, print an error message.
2. Write a program where the user inputs a letter. Use a switch-case to determine whether the letter is a
vowel (a, e, i, o, u) or a consonant. Make sure your program can handle both uppercase and lowercase
letters. If the input is not a letter, indicate that it's an invalid input.
3. Simulate traffic light signals in a program. Ask the user to input 'R', 'Y', or 'G', representing red, yellow,
and green lights, respectively. Use a switch-case to print actions for each signal (e.g., 'R' for "Stop", 'Y'
for "Slow Down", 'G' for "Go").
4. Create a program that asks the user to input a month number (1 for January, 2 for February, etc.). Use a
switch-case to print the number of days in the specified month. Assume it is not a leap year for
February.

13 | P a g e

You might also like