0% found this document useful (0 votes)
3 views10 pages

Lab Report - 3

This lab report explores the use of the switch-case control structure in C programming through three practical exercises: checking if a character is a vowel or consonant, implementing a simple calculator, and calculating student grades based on marks. Each program demonstrates how switch-case simplifies decision-making and enhances code readability. The report concludes with key takeaways on the importance of control structures and common mistakes to avoid in programming.

Uploaded by

2251081143
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)
3 views10 pages

Lab Report - 3

This lab report explores the use of the switch-case control structure in C programming through three practical exercises: checking if a character is a vowel or consonant, implementing a simple calculator, and calculating student grades based on marks. Each program demonstrates how switch-case simplifies decision-making and enhances code readability. The report concludes with key takeaways on the importance of control structures and common mistakes to avoid in programming.

Uploaded by

2251081143
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/ 10

CSE Lab Report-3

Submitted by:
Name: Md. Abid Al Azmain Shakil
ID: 2251081143
Batch: 63(D)
Department: CSE

Submitted to:
Name: Noor Easrib Tiba
Submission Date: 06/03/2025
Department of CSE
Uttara University
Introduction

C programming is one of the most powerful and widely used programming languages, known for
its simplicity, efficiency, and flexibility. It is widely used for system-level programming and
embedded systems, as well as applications requiring high-performance execution. In this set of
programs, we will explore the use of the switch-case control structure, which allows us to
implement decision-making logic based on different cases. The first program checks whether a
given alphabet is a vowel or consonant, the second implements a simple calculator to perform
arithmetic operations, and the third calculates a student's grade based on their marks. These
exercises highlight the importance of control structures in decision-making and the effective use
of C language features to solve everyday problems.

Description

1. Vowel or Consonant Checker Using Switch-Case: The first program takes a character as input
and checks whether it is a vowel or consonant using the switch-case statement. The switch-
case is used to match the character with the vowel letters ('a', 'e', 'i', 'o', 'u') in both
uppercase and lowercase. If the input character matches any of these vowels, the program
will output "Vowel." If it does not match any vowel, the program will output "Consonant."
This program shows how switch-case can be used for handling multiple conditions with a
clean and readable approach, simplifying the logic of checking for specific characters.
2. Simple Calculator Using Switch-Case: The second program implements a basic calculator that
performs five arithmetic operations: addition, subtraction, multiplication, division, and
modulus. The user provides two integer inputs, and the program offers a menu of
operations. The switch-case statement is used to handle each operation by matching the
user's choice and executing the corresponding arithmetic operation. For example, if the user
selects "1" for addition, the program will add the two numbers, and similarly for the other
operations. This program demonstrates the power of switch-case for executing different
blocks of code based on user input, simplifying decision-making in interactive applications.
3. Grade Calculator Using Switch-Case: The third program calculates the grade of a student
based on their marks using the switch-case statement. The user enters their marks, and the
program divides the marks by 10 to determine the grade. For example, if the marks fall
between 90 and 100, the student receives an "A"; between 80 and 89, the grade is "B", and
so on. The switch-case statement matches the range of marks and assigns the appropriate
grade. This program helps demonstrate how to use switch-case for range-based decision-
making and how to manage multiple possible outcomes efficiently.
Key Points to Note:

• Switch-Case Statement: The switch-case statement provides an efficient way to handle


multiple conditions. It evaluates an expression and compares it with the values defined in
case labels.
• User Input Handling: These programs use scanf() to take input from the user and printf()
to display the output.
• Control Flow: The use of switch-case enhances the clarity and organization of the
decision-making process in all three programs.

Experiment No. 1

Write a C program to check whether an alphabet is vowel or consonant by using switch case
statement.

Source Code

#include<stdio.h>
int main()
{
char ch;

//Taking input from user


printf("Enter a Character : ");
scanf("%c", &ch);

switch(ch)
{
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is a consonant.\n", ch);
}
return 0;
}

Output

Experiment No. 2

Write a C program to develop a calculator that will perform the following operations by using
switch case statement.
o a+b
o a-b
o a*b
o a/b
o a%b

Source Code
#include<stdio.h>
int main()
{
char opr;
int num1,num2,sum,sub,mul,rem;
float div;
//Taking input from user
printf("Enter an Operator(+-*/%) : ");
scanf("%c", &opr);
printf("Enter the value of Number 1 : ");
scanf("%d", &num1);
printf("Enter the value of Number 2 : ");
scanf("%d", &num2);

switch(opr)
{
case '+':
sum = num1 + num2;
printf("The summation of two number is : %d\n", sum);
break;
case '-':
sub = num1 - num2;
printf("The subtraction of two number is : %d\n", sub);
break;
case '*':
mul = num1 * num2;
printf("The multiplication of two number is : %d\n", mul);
break;
case '/':
div =(float) num1 / num2;
printf("The division of two number is : %.2f\n", div);
break;
case '%':
rem = num1 % num2;
printf("The remainder when Number 1 is divided by Number 2 is : %d\n", rem);
break;
default:
printf("Invalid Operation!\n");
}
return 0;
}

Output

Experiment No. 3

Write a C program to develop a grade calculator to get student's grade by using switch case
statement.
Source Code

#include<stdio.h>
int main()
{
int marks;

//Taking input from user


printf("Enter your marks(0-100): ");
scanf("%d", &marks);

//Input validation
if(marks<0 || marks>100)
{
printf("Invalid marks! Please enter a number between o and 100.\n ");
return 1; //Exit with an error
}

//Switch case for grade evaluation


switch(marks/5)
{
case 20: //100 marks case
case 19:
case 18:
case 17:
case 16:
printf(“Grade : A+(4.00)\n”);
break;
case 15:
printf(“Grade : A(3.75)\n”);
break;
case 14:
printf(“Grade : A-(3.50)\n”);
break;
case 13:
printf(“Grade : B+(3.25)\n”);
break;
case 12:
printf(“Grade : B(3.00)\n”);
break;
case 11:
printf(“Grade : B-(2.75)\n”);
break;
case 10:
printf(“Grade : C+(2.50)\n”);
break;
case 9:
printf(“Grade : C(2.25)\n”);
break;
case 8:
printf(“Grade : D(2.00)\n”);
break;

default:
printf(“Grade : F(Fail,0.00)\n”);
}

return 0;
}

Output

Conclusion

These programs give a solid understanding of how to use the switch-case control structure in C
programming for handling multiple possible conditions. The switch-case statement simplifies
decision-making, making the code more readable and easier to maintain. Through these
exercises, we have learned how to:

• Use switch-case to handle multiple conditions without resorting to multiple if-else


statements.
• Develop interactive programs that perform operations based on user input.
• Apply basic arithmetic operations and conditions to solve practical problems, such as
checking for vowels or consonants, performing arithmetic calculations, and determining
grades.

From these programs, we can conclude that control flow constructs, such as switch-case, are
essential for building interactive applications and handling a variety of conditions. They provide a
cleaner and more structured way to handle decisions, especially when the number of possible
conditions increases.

Key Mistakes to Avoid:


• Forgetting to include a break statement in switch-case: Without the break, the program
may fall through to the next case and execute unintended code.
• Incorrectly handling user input: Always validate input to avoid invalid data causing issues,
such as dividing by zero in the calculator program.
• Not considering case sensitivity: When dealing with characters, remember that switch-
case is case-sensitive. This means that 'a' and 'A' are treated as different values, which
could lead to logical errors.

In conclusion, these exercises provide valuable insight into handling different conditions and
operations efficiently in C, which will serve as a foundation for tackling more complex problems
in the future.

The End

You might also like