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



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

Answer / 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

More C Interview Questions

what is pointer ? what is the use of pointer?

6 Answers   Infosys,


How to write c functions that modify head pointer of a linked list?

1 Answers  


What is the difference between procedural and declarative language?

1 Answers  


f(x,y,z) { y = y+1; z = z+x; } main() { int a,b; a = 2 b = 2; f(a+b,a,a); print a; } what is the value of 'a' printed

5 Answers  


Determine the code below, tell me exactly how many times is the operation sum++ performed ? for ( i = 0; i < 100; i++ ) for ( j = 100; j > 100 - i; j--) sum++;

5 Answers   ITCO, Wipro,


How are variables declared in c?

1 Answers  


Explain what would happen to x in this expression: x += 15; (assuming the value of x is 5)

1 Answers  


Will Macros support multiple arguments ?

7 Answers   Oracle,


Which is better pointer or array?

1 Answers  


diff between exptected result and requirement?

1 Answers   HCL,


Does c have function or method?

1 Answers  


what is linkage error when it occurs in c program

3 Answers  


Categories