This paper deals with a novel method for solving optimal control problems based on genetic programming. This approach produces some trial solutions and seeks the best of them. If the solution cannot be expressed in a closed analytical form then our method produces an approximation with a controlled level of accuracy. Using numerical examples, we will demonstrate how to use the results.