code.cpp 630 B

123456789101112131415161718192021
  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. * a change.
  5. */
  6. std::string operation(int num1, int num2) {
  7. std::string result = "Invalid";
  8. if (num1 + num2 == 24) {
  9. result = "added";
  10. }
  11. else if ((num1 - num2 == 24) || (num2 - num1 == 24)) {
  12. result = "subtracted";
  13. }
  14. else if (num1 * num2 == 24) {
  15. result = "multiplied";
  16. }
  17. else if (((0 != num2) && (num1 / num2 == 24)) || ((0 != num1) && (num2 / num1 == 24))) {
  18. result = "divided";
  19. }
  20. return result;
  21. }