0% found this document useful (0 votes)
2 views5 pages

Experiment 1

Superposition theorem book

Uploaded by

sanket.thorat804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Experiment 1

Superposition theorem book

Uploaded by

sanket.thorat804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Information Technology C Programming WCE

Name of Student: Ram Raghavendra Acharya


Class: IT
Batch: B1
Roll No: 246109018
Aided/ Unaided: Unaided

Experiment No: 1
Title:
Program to simulate simple calculator that performs basic tasks such as
addition, subtraction, multiplication and division.
Aim:
To make a simple calculator
Objective:
 To understand the usage of switch-case and decision-making constructs
in C.
 To implement arithmetic operations in a C program.
 To simulate a calculator that can add, subtract, multiply, and divide two
numbers.

Theory:
 The switch statement in C allows multi-way branching based on the value
of an integral expression (like int or char).
 It consists of multiple case labels, each representing a constant value,
with optional break statements to prevent fall-through to subsequent
cases. A default case can catch unmatched values. While switch can be
more efficient than multiple if-else statements, it is limited to integral
types and cannot perform range checks. Variables declared within a case
are scoped
Information Technology C Programming WCE
only to that case. Overall, the switch statement provides a clear way to
handle complex conditional logic.
Code:
#include <stdio.h>

int main() {
char operator;
double num1, num2, result;

// Input operator from user


printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);

// Input two numbers from user


printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);

// Perform operation based on the operator


switch(operator) {
case '+':
result = num1 + num2;
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);
break;
case '-':
result = num1 - num2;

printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);


Information Technology C Programming WCE
break;
case '*':
result = num1 * num2;
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);
break;
case '/':

if(num2 != 0) {
result = num1 / num2;
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Error! Invalid operator.\n");
}

return 0;
}

Snapshot of Output :
Information Technology C Programming WCE
Addition

Subtraction

Multiplication

Division

Division By Zero

Invalid Operator

Conclusion :
Information Technology C Programming WCE
The C program successfully simulates a simple calculator that performs
addition, subtraction, multiplication, and division based on user input. By using
the switch-case construct, different operations are performed efficiently. The
program also handles division by zero to avoid runtime errors.

You might also like