Ex.
No: 5
Develop a simple scientific calculator using Switch case statement
Aim:
To develop a simple scientific calculator using a switch-case statement that performs basic
arithmetic operations such as addition, subtraction, multiplication, division, and modulus.
Algorithm:
1. Start.
2. Display a menu with options for different operations (addition, subtraction, multiplication,
division, and modulus).
3. Read the user's choice of operation.
4. Based on the user's choice, use the switch-case statement to perform the respective
operation:
o Case 1: Addition.
o Case 2: Subtraction.
o Case 3: Multiplication.
o Case 4: Division.
o Case 5: Modulus.
5. For each operation, ask the user to input two numbers.
6. Perform the chosen operation and display the result.
7. If the user enters an invalid choice, display an error message.
8. Stop.
Pseudocode:
sql
Copy code
START
DISPLAY menu with options for addition, subtraction, multiplication, division, modulus
INPUT choice
SWITCH choice
CASE 1:
INPUT two numbers
ADD the numbers
PRINT result
BREAK
CASE 2:
INPUT two numbers
SUBTRACT the numbers
PRINT result
BREAK
CASE 3:
INPUT two numbers
MULTIPLY the numbers
PRINT result
BREAK
CASE 4:
INPUT two numbers
DIVIDE the numbers
IF second number is zero THEN
PRINT error message
ELSE
PRINT result
BREAK
CASE 5:
INPUT two numbers
FIND modulus
PRINT result
BREAK
DEFAULT:
PRINT invalid choice
STOP
Flowchart:
C Program:
Copy code
#include <stdio.h>
int main() {
int choice;
double num1, num2, result;
// Display the menu
printf("Simple Scientific Calculator\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus\n");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
// Switch-case for different operations
switch (choice) {
case 1:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 + num2;
printf("Result: %.2lf\n", result);
break;
case 2:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 - num2;
printf("Result: %.2lf\n", result);
break;
case 3:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 * num2;
printf("Result: %.2lf\n", result);
break;
case 4:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
if (num2 == 0) {
printf("Error: Division by zero is not allowed.\n");
} else {
result = num1 / num2;
printf("Result: %.2lf\n", result);
break;
case 5:
printf("Enter two integers: ");
int int1, int2;
scanf("%d %d", &int1, &int2);
if (int2 == 0) {
printf("Error: Modulus by zero is not allowed.\n");
} else {
result = int1 % int2;
printf("Result: %d\n", (int)result);
break;
default:
printf("Invalid choice.\n");
return 0;
Sample Output:
Example 1: Addition
markdown
Copy code
Simple Scientific Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 1
Enter two numbers: 10 20
Result: 30.00
Example 2: Division with Error Handling
markdown
Copy code
Simple Scientific Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 4
Enter two numbers: 10 0
Error: Division by zero is not allowed.
Example 3: Modulus
markdown
Copy code
Simple Scientific Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 5
Enter two integers: 10 3
Result: 1
Result:
The program successfully implements a simple scientific calculator using the switch-case statement.
It can perform addition, subtraction, multiplication, division, and modulus operations based on user
input. The program also handles invalid choices and division/modulus by zero errors.