Software size and complexity continue to grow and find new applications, such as safety assurance systems for nuclear power generators, electronic transfer systems for banking, and manned spacecraft. To achieve ultrareliability in computing, it is necessary to adopt the strategy of defensive programming based on redundancy. Redundant versions, however, require additional resources, e.g., additional costs in terms of programming effort, time needed for design and testing, and hardware requirements. This article addresses the optimization issue for the cost of N-version programming (NVP) and majority systems. The optimization problems are formulated and solved for the minimum expected cost of NVP and majority systems subject to the desired reliability level. The problems of maximizing the reliability of the NVP and majority systems subject to a constraint on expected system cost are also obtained. Numerical examples are provided to illustrate the results.