adspace


Stimulate calculator using Switch-case-default statement for
two numbers

Answer Posted / Anil Kumar Ojha

Here's a simple example in C:
```c
#include <stdio.h>

void main() {
int num1, num2, choice;

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

printf("Enter operation number:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exponentn");
scanf("%d", &choice);

switch(choice) {
case 1: printf("%d + %d = %d", num1, num2, num1 + num2); break;
case 2: printf("%d - %d = %d", num1, num2, num1 - num2); break;
case 3: printf("%d * %d = %d", num1, num2, num1 * num2); break;
case 4: if(num2 != 0) printf("%d / %d = %f", num1, num2, (float)num1 / num2); else printf("Error! Division by zero is not allowed."); break;
case 5: printf("%d mod %d = %d", num1, num2, num1 % num2); break;
case 6: printf("%d ^ %d = %d", num1, num2, pow(num1, num2)); break;
default: printf("Invalid operation number. Please enter a valid option.");
}
}
```
This code prompts the user to input two numbers and choose an operation, then it calculates the result using a switch-case statement.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Sir,please help me out with the code of this question. Write an interactive C program that will encode or decode multiple lines of text. Store the encoded text within a data file, so that it can be retrieved and decoded at any time. The program should include the following features: (a) Enter text from the keyboard, encode the text and store the encoded text in a data file. (b) Retrieve the encoded text and display it in its encoded form. (c) Retrieve the encoded text, decode it and then display the decoded text. (d) End the computation. Test the program using several lines of text of your choice.

2397


In C language, the variables NAME, name, and Name are all the same. TRUE or FALSE?

1295


hi folks i m approching for h1 b interview on monday 8th of august at montreal and i m having little problem in my approval notice abt my bithdate my employer has made a mistake while applying it is 12th january and istead of that he had done 18 the of january do any body have any solution for that if yes how can i prove my visa officer abt my real birthdate it urgent please let me know guys thaks dipesh patel

1942


what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;

2514


The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?

1358


Write a C program that will accept a hexadecimal number as input and then display a menu that will permit any of the following operations to be carried out: Display the hexadecimal equivalent of the one's complement. (b) Carry out a masking operation and then display the hexadecimal equivalent of the result. (c) Carry out a bit shifting operation and then display the hexadecimal equivalent of the result. (d) Exit. If the masking operation is selected, prompt the user lor the type of operation (bitwise and, bitwise exclusive or, or bitwise or) and then a (hexadecimal) value for the mask. If the bit shifting operation is selected. prompt the user for the type of shift (left or right), and then the number of bits. Test the program with several different (hexadecimal) input values of your own choice.

5446


swap 2 numbers without using third variable?

1208


`write a program to display the recomended action depends on a color of trafic light using nested if statments

2214


what is ur strangth & weekness

2588


What is pointers in c with example?

1167


Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?

1181


How can I implement sets or arrays of bits?

1117


What is pointer to pointer in c with example?

1197


write a c program to find the sum of five entered numbers using an array named number

2245


What are pointers? What are different types of pointers?

1263