0% found this document useful (0 votes)
102 views

Creating A Calculator Using Switch Case Statement With C Program

The document describes a C program that creates a basic calculator using switch-case statements. It takes in an operator and two numbers from the user, uses a switch-case statement to perform the operation, and prints the result. The program includes the source code, which uses scanf to get input and printf to output the result. It correctly calculates multiplication in this example by matching the operator to the '*' case.

Uploaded by

Mohammad Saimon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Creating A Calculator Using Switch Case Statement With C Program

The document describes a C program that creates a basic calculator using switch-case statements. It takes in an operator and two numbers from the user, uses a switch-case statement to perform the operation, and prints the result. The program includes the source code, which uses scanf to get input and printf to output the result. It correctly calculates multiplication in this example by matching the operator to the '*' case.

Uploaded by

Mohammad Saimon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

You might also like