Sunday 16 April 2017

Program for arithmetic calculator using switch

Program to demo arithmetic calculator using switch


#include<stdio.h>
void main()
{
             int a, b;
             float res;
             char op;
             printf("\nEnter a op b :");
             scanf("%d %c %d", &a, &op, &b);
             switch(op)
            {
                         case  '+' :       res = a+b;
                                              break;
                         case  '-' :        res = a-b;
                                              break;
                         case  '*' :        res = a * b;
                                              break;
                          case  '/' :        if(!b)
                                                      printf("\n Divide by zero error");
                                               else
                                                      res = (float)a/b;
                                               break;
                          case  '%' :      res = a % b;
                                               break;
                          default:        printf("\nGive valid operator+");
                                              break;
            }
            printf("\n Result = %f", res);
}

Output:

Enter a op b : 1   /    2
Result = 0.500000

No comments:

Post a Comment