Advertisement
Dido09

C - Calculator

May 28th, 2022
593
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. float add(float num1, float num2);
  4. float sub(float num1, float num2);
  5. float mult(float num1, float num2);
  6. float div(float num1, float num2);
  7.  
  8. int main(){
  9.  
  10. char op;
  11. float num1, num2, result = 0.0f;
  12.  
  13. printf("Enter number, operator and number: ");
  14. scanf("%f %c %f", &num1, &op, &num2);
  15.  
  16. switch(op){
  17. case '+':
  18.     result = add(num1, num2);
  19.     break;
  20. case '-':
  21.     result = sub(num1, num2);
  22.     break;
  23. case '*':
  24.     result = num1 * num2;
  25.     break;
  26. case '/':
  27.     result = num1 / num2;
  28. default:
  29.     printf("Enter a valid operator");
  30. }
  31.  
  32. printf("%.2f %c %.2f = %.2f", num1, op, num2, result);
  33. return 0;
  34. }
  35.  
  36. float add(float num1, float num2){
  37.     return num1 + num2;
  38. }
  39.  
  40. float sub(float num1, float num2){
  41.     return num1 - num2;
  42. }
  43.  
  44. float mult(float num1, float num2){
  45.     return num1 * num2;
  46. }
  47.  
  48. float div(float num1, float num2){
  49.     return num1 / num2;
  50. }
  51.  
Advertisement
RAW Paste Data Copied
Advertisement