Advertisement
tonygms3

Calculator with switch statement

Oct 25th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. /********************************************************/
  2. //Nicholas Tony Gomes
  3. //Calculator with switch statement
  4. //and functions
  5. //2:15 pm oct 26 2017
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <math.h>
  10. void i_o();
  11.  
  12. double add(double x, double y)
  13. {
  14.     return x+y;
  15. }
  16. double sub(double x, double y)
  17. {
  18.     return x-y;
  19. }
  20. double multiply(double x, double y)
  21. {
  22.     return x*y;
  23. }
  24. double division(double x, double y)
  25. {
  26.     return x/y;
  27. }
  28. double remain(double x, double y)
  29. {
  30.     return fmod(x,y);
  31. }
  32. void i_o()
  33. {
  34.     printf("Press 1 for Addition\n");
  35.     printf("Press 2 for Subtraction\n");
  36.     printf("Press 3 for Multiply\n");
  37.     printf("Press 4 for Division\n");
  38.     printf("Press 5 for finding the Remainder\n");
  39. }
  40. int main()
  41. {
  42.     double num_input, num2_input;
  43.     int num_operator,counter;
  44.  
  45.    for(counter=1;counter<=5;counter++){
  46.     printf("\nEnter two numbers to calculate\n");
  47.     scanf("%lf%lf",&num_input,&num2_input);
  48.     i_o();
  49.     scanf("%d",&num_operator);
  50.  
  51.     switch(num_operator)
  52.     {
  53.         case 1:
  54.             printf("%.2lf",add(num_input,num2_input));
  55.         break;
  56.  
  57.         case 2:
  58.             printf("%.2lf",sub(num_input,num2_input));
  59.         break;
  60.  
  61.         case 3:
  62.             printf("%.2lf",multiply(num_input,num2_input));
  63.         break;
  64.  
  65.         case 4:
  66.             printf("%.2lf",division(num_input,num2_input));
  67.         break;
  68.  
  69.         case 5:
  70.             printf("%.2lf",remain(num_input,num2_input));
  71.         break;
  72.  
  73.  
  74.         default:
  75.             printf("Wrong input!!!\n");
  76.         break;
  77.     }
  78.  
  79.  
  80.     }
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement