19.Write a program in C to perform basic arithmetic operations based on user input using 'switch-case'.
Purpose:
The purpose of the program is to allow users to perform basic arithmetic operations (addition, subtraction, multiplication, division) on two numbers based on their input. This program provides a simple calculator functionality to perform mathematical calculations efficiently.
Agenda:
Prompt the user to enter two numbers and select an arithmetic operation addition, subtraction, multiplication, division.
Use a switch-case statement to determine the selected operation and perform the corresponding arithmetic calculation.
Display the result of the arithmetic operation to the user.
Provide an option for the user to perform additional calculations or exit the program.
Real-life applications:
This program can be used in various real-life scenarios where quick calculations are required, such as in financial transactions, budget planning, engineering calculations, and scientific research. It can also be used in educational settings to help students practice arithmetic operations.
A program in C to perform basic arithmetic operations based on user input using switch case.
/* C language program to perform basic arithmetic operations
based on user input using switch case.*/
#include <stdio.h>
int main()
{
char operator;
double num1, num2;
printf("\nEnter an operator (+, -, *, /) ");
scanf("%c", &operator);
printf("\nEnter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch(operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf", num1, num2, num1 + num2);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf", num1, num2, num1 - num2);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf", num1, num2, num1 * num2);
break;
case '/':
if(num2 != 0)
printf("%.2lf / %.2lf = %.2lf", num1, num2, num1 / num2);
else
printf("Error! Division by zero is not allowed.");
break;
default:
printf("Error! Invalid operator.");
}
return 0;
}
Output
Enter an operator (+, -, *, /):
+
Enter two numbers:
5
4
5.00+4.00=9.00