Lab Report - 3
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:
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;
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;
//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
}
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:
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.
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