Programs - Maths24


Aim

Once upon a time lived two children, one good at programming and one with a very nasty maths teacher. One day the mathematically challenged child came home with a very hard Maths24 problem and spent hours trying to solve it. After a while she gave up and burst into tears. Her brother being kind, gentle and lazy promised that he would not rest until he found the answer for her, but he had a trick up his sleeve.

Method

The program stars by displaying the introduction messages, it then asks for the four magic numbers which it stores as strings, fractions can be entered as 1/2 or 3/5. It then converts each of them into real numbers for working. At this stage all is set, the program can get on with some number crunching.

It runs through all possible combinations of order, combinations of actions (add, subtract, multiply, divide and factorial), and combinations of bomdas (order of operation). For each of the possibilities it works out the answer and if the answer is 24 (allowing for rounding off) then print out how we got to that answer. If the user typed in fractions it will not print the often horrible real number but the fraction. Obviously there will be more than one way to solve the problem if we bracket the numbers differently, but then there’s nothing wrong with that.