johnmarkthegreat

Sample Program with Multiple Functions

Mar 5th, 2024 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | Source Code | 0 0
  1. #include <stdio.h>
  2.  
  3. int computeSum(int num1, int num2);
  4. int computeDifference(int num1, int num2);
  5. int computeProduct(int num1, int num2);
  6. float computeQuotient(int num1, int num2);
  7.  
  8. int main() {
  9. char op;
  10. int x, y;
  11. float z;
  12.  
  13. scanf(" %c", &op);
  14. scanf("%d %d", &x, &y);
  15.  
  16. switch (op) {
  17. case '+':
  18. printf("Sum = %d", computeSum(x, y));
  19. break;
  20.  
  21. case '-':
  22. printf("Difference = %d", computeDifference(x, y));
  23. break;
  24.  
  25. case '*':
  26. printf("Product = %d", computeProduct(x, y));
  27. break;
  28.  
  29. case '/':
  30. z = computeQuotient(x, y);
  31. printf("Quotient = %.2f", z);
  32. break;
  33.  
  34. default:
  35. printf("Invalid operator!");
  36. break;
  37. }
  38. }
  39.  
  40. int computeSum(int num1, int num2) {
  41. return num1 + num2;
  42. }
  43.  
  44. int computeDifference(int num1, int num2) {
  45. return num1 - num2;
  46. }
  47.  
  48. int computeProduct(int num1, int num2) {
  49. return num1 * num2;
  50. }
  51.  
  52. float computeQuotient(int num1, int num2) {
  53. return (float)num1 / (float)num2;
  54. }
Tags: CSIT 122
Advertisement
Add Comment
Please, Sign In to add comment