{ A destination to learn coding }

Calculator using a switch statement


#include <stdio.h>
int main()
{
int a,b,ans,op;
printf("A calculator for calculating 2 numbers\nEnter the 2 numbers\n");
scanf("%d%d",&a,&b);

printf("Select option\n1.Addition\n2.Subtraction\n3.Multiplication\n");
scanf("%d",&op);

switch(op)
{
  case 1:
   ans=a+b;
   printf("The answer is %d\n",ans);
   break;

  case 2:
   ans=a-b;
   printf("The answer is %d\n",ans);
   break;

  case 3:
   ans=a*b;
   printf("The answer is %d\n",ans);
   break;

  default: printf("Invalid input\n");
}
return 0;
}