Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Saturday, July 16, 2022

C program to create a simple calculator using the switch statement

 C program to create a simple calculator using the switch statement



C program to create a simple calculator using the switch statement


Introduction:

In programming, a calculator is a basic tool that performs various arithmetic operations. In this article, we will focus on a C program that can create a simple calculator using the switch statement. The calculator will be able to perform addition, subtraction, multiplication, and division.

Before we dive into the code, let's briefly discuss the concept of the switch statement. The switch statement is a control structure in programming that allows us to select one of several code blocks to be executed. It is often used in situations where we have a limited number of options to choose from.


Now, let's take a look at the C program that can create a simple calculator using the switch statement:



#include<stdio.h>
int main()
{
float a,b, r;
char op;
printf("Available Operations.\n");
printf("+ for Addition.\n");
printf("- for Subtraction.\n");
printf("* for Multiplication.\n");
printf("/ for Division.\n");
printf("Which Operation?\n");
scanf("%c", &op);
switch(op)
{
case '+': printf("Enter two numbers:\n");
scanf("%f%f",&a, &b);
r = a+b;
printf("%f + %f = %f", a, b, r);
break;
case '-': printf("Enter two numbers:\n");
scanf("%f%f",&a, &b);
r = a-b;
printf("%f - %f = %f", a, b, r);
break;
case '*': printf("Enter two numbers:\n");
scanf("%f%f",&a, &b);
r = a*b;
printf("%f * %f = %f", a, b, r);
break;
case '/': printf("Enter two numbers:\n");
scanf("%f%f",&a, &b);
if(b!=0)
{
r = a/b;
printf("%f/%f = %f",a,b,r);
}
else
{
printf("Division not possible.");
}
break;
default: printf("Invalid Operation.");
break;
}
return 0;
}




Program Explanation:

In the above program, we first declare two float variables num1 and num2, and a char variable operator. We then use the printf() and scanf() functions to prompt the user to input two numbers and an operator.

Next, we use a switch statement to perform the appropriate arithmetic operation based on the value of operator. The case statements handle each of the four possible operators (+, -, *, and /), and the default case is used to handle any invalid operators.



Inside each case, we use the printf() function to display the result of the corresponding arithmetic operation. If the user tries to divide by zero, we display an error message instead.

Output:




Conclusion:

In conclusion, this C program demonstrates how to use a switch statement to create a basic simple calculator that performs addition, subtraction, multiplication, and division on two input numbers. It can be easily modified to handle additional arithmetic operations or more complex input validations.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages