In: Computer Science
in C programing language
Two numbers entered from the command line (example:
calculate.exe 2 + 2)
Add, subtract, divide and multiply by (+, -, /, *) operators
Write the C program that makes the calculation using
switch-case.
hint: operator [2][0] comes from index .
Note: here expression stores in argv[1] not argv[2] for my compiler
Source Code:
#include <stdio.h>
int main(int argc, char const *argv[])
{
int operand1,operand2,operator;
operand1=argv[1][0]-'0';
operand2=argv[1][2]-'0';
operator=argv[1][1];
int result=0;
switch(operator)
{
case '+':
result=operand1+operand2;
break;
case '-':
result=operand1-operand2;
break;
case '*':
result=operand1*operand2;
break;
case '/':
result=operand1/operand2;
break;
}
printf("Result of %s is %d\n",argv[1],result);
}
Sample input and output: