Software Development Exam  >  Software Development Questions  >  Write a program in c++ to perform a mini calc... Start Learning for Free
Write a program in c++ to perform a mini calculator using switch statement.
Most Upvoted Answer
Write a program in c++ to perform a mini calculator using switch state...
**Mini Calculator using Switch Statement in C**

Switch statements are used to select one of the several blocks of code to be executed. In this program, we are going to perform a mini calculator using switch statement in C.

**Steps to Perform Mini Calculator using Switch Statement in C**

1. First, we need to declare the variables to store the values of two operands and the operator.
2. Then, we need to display a menu of options for the user to select the operation they want to perform.
3. We can use scanf() function to take the input from the user for the operands and the operator.
4. Now, we can use switch statement to execute the required operation based on the selected option.
5. We can use printf() function to display the result of the operation.

**Code Implementation**

Here is the code for performing a mini calculator using switch statement in C.

```
#include

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

printf("Enter two operands: ");
scanf("%f %f", &num1, &num2);

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

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

case '-':
result = num1 - num2;
printf("%.2f - %.2f = %.2f", num1, num2, result);
break;

case '*':
result = num1 * num2;
printf("%.2f * %.2f = %.2f", num1, num2, result);
break;

case '/':
result = num1 / num2;
printf("%.2f / %.2f = %.2f", num1, num2, result);
break;

default:
printf("Invalid operator");
break;
}

return 0;
}
```

**Explanation of Code**

- In the first line of the code, we have included the header file stdio.h which contains the standard input/output functions.
- Then, we have declared the variables num1, num2, result, and operator of type float and char respectively.
- We have used printf() function to display the message to the user to enter the two operands.
- We have used scanf() function to take the input from the user for the two operands.
- Then, we have used printf() function to display the message to the user to enter the operator.
- We have used scanf() function to take the input from the user for the operator.
- We have used switch statement to execute the required operation based on the selected operator.
- We have used printf() function to display the result of the operation.
- In the end, we have returned 0 to the operating system to indicate that the program has executed successfully.
Explore Courses for Software Development exam

Similar Software Development Doubts

Read the given passage and answer the following questions.The concept of Universal Basic Income (UBI) has gained traction in recent years as a radical approach to redefining societal welfare and addressing income inequality. UBI proposes that every citizen, regardless of employment status, wealth, or social standing, receives a regular, unconditional sum of money from the government. This revolutionary idea challenges traditional welfare systems, which are often conditional, bureaucratic, and stigmatizing to recipients. By suggesting a model that is universal and unconditional, UBI raises intriguing moral and rights-based questions about societal obligations, individual freedom, and the nature of social justice.Advocates argue that UBI embodies a recognition of basic human rights, ensuring that every person has the means to secure a minimum standard of living. In a world where technological advancement threatens to disrupt labor markets and exacerbate unemployment, UBI is seen as a tool to safeguard individuals against economic uncertainties, granting them the freedom to pursue education, engage in creative endeavors, or care for family members without the fear of extreme poverty.Critics, however, question the morality of providing individuals with income unlinked from work, suggesting it could undermine the work ethic and create disincentives for productive engagement in society. They also raise concerns about the financial sustainability of such programs and the potential for inflationary effects that could erode the actual value of the income provided.The UBI debate, therefore, not only examines the mechanics of implementing such a program but also delves into deeper philosophical inquiries about what societies owe their members, the role of government in securing economic stability, and the definitions of freedom and dignity in the modern world.Which statement is accurate regarding the UBI debate?

Top Courses for Software Development

Write a program in c++ to perform a mini calculator using switch statement.
Question Description
Write a program in c++ to perform a mini calculator using switch statement. for Software Development 2025 is part of Software Development preparation. The Question and answers have been prepared according to the Software Development exam syllabus. Information about Write a program in c++ to perform a mini calculator using switch statement. covers all topics & solutions for Software Development 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Write a program in c++ to perform a mini calculator using switch statement..
Solutions for Write a program in c++ to perform a mini calculator using switch statement. in English & in Hindi are available as part of our courses for Software Development. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.
Here you can find the meaning of Write a program in c++ to perform a mini calculator using switch statement. defined & explained in the simplest way possible. Besides giving the explanation of Write a program in c++ to perform a mini calculator using switch statement., a detailed solution for Write a program in c++ to perform a mini calculator using switch statement. has been provided alongside types of Write a program in c++ to perform a mini calculator using switch statement. theory, EduRev gives you an ample number of questions to practice Write a program in c++ to perform a mini calculator using switch statement. tests, examples and also practice Software Development tests.
Explore Courses for Software Development exam

Top Courses for Software Development

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev