Name: Asif Mohammad Saimon
Title of Experiment: Write a C program to create a
calculator using Switch-Case statement.
Student Id =221002112
Course code: 112.1
Date: 26/03/2022
Course name: Computer Fundamentals and
Programming Basic Laboratory.
Submitted To: Moumita Sen Sarma.
Creating a calculator using switch case
statement with C program
INTRODUCTION: The switch statement allows us to
execute one code block among many alternatives. You
can do the same thing with the if...else.. if ladder. The
break statement ends the loop immediately when it is
encountered.
Source code:
#include <stdio.h>
int main()
{
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two number: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
Coding:
Output result :
Discussion: In this experiment the * operator entered by
the user is stored in op. And, the two operands, 5 and 7
are stored in first and second respectively. Since the
operator * matches case '*' the control of the program
jumps to this statement calculates the product and
displays it on the screen. To make our output look
cleaner, we have simply limited the output to one
decimal place using the code %.1lf.