Source code:
/*Write a C program for calculator program with Basic operations (+, -, *, /,%) of two numbers using switch statement. */
#include <stdio.h>
int main()
{
int num1,num2;
float result =0;
char ch; /*to store operator choice */
printf("Enter the 1st number: ");
scanf("%d",&num1);
printf("Enter the 2nd number: ");
scanf("%d",&num2);
printf("Enter the operation(+,-,*,/) : ");
scanf(" %c",&ch); /* Operation to perform +,-,*,/ */
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
return 0;
}
No comments:
Post a Comment