code.cpp 618 B

1234567891011121314151617181920
  1. /* Write a function that takes two numbers and returns if they
  2. * should be added, subtracted, multiplied or divided to get 24.
  3. * If none of the operations can give 24, return "Invalid".
  4. */
  5. std::string operation(int num1, int num2) {
  6. std::string result = "Invalid";
  7. if (num1 + num2 == 24) {
  8. result = "added";
  9. }
  10. else if ((num1 - num2 == 24) || (num2 - num1 == 24)) {
  11. result = "subtracted";
  12. }
  13. else if (num1 * num2 == 24) {
  14. result = "multiplied";
  15. }
  16. else if (((0 != num2) && (num1 / num2 == 24)) || ((0 != num1) && (num2 / num1 == 24))) {
  17. result = "divided";
  18. }
  19. return result;
  20. }