Calculations


Math can get complicated quickly. To help with this complexity, we have created tools to help us solve math problems. Abacuses are devices used to keep track of numbers and complex calculations. Historians have found evidence of their use thousands of years ago and they are still in use today.

In the 17th century, Blaise Pascal created the first mechanical calculator. This device uses gears and clockwork to solve math equations. In time, Charles Babbage and Ada Lovelace developed the difference engine, which is considered to be the world’s first computer.

Calculations

Today’s computers continue to help us solve our math problems, and in this CodeNewbie Challenge you will create a program that will solve mathematical equations.

As always, the challenge is split into three levels. Please choose your level based on your available time and your comfort level with the challenge.

Level 1

For this level, write a program that will add two numbers. When it is run it will ask for two numbers and output their sum.

Specification

Given your program that asks for two numbers

When you enter 3 and 5

Then it will output 8

Level 2

To complete level two, create a program that will perform addition or subtraction. It will ask for two numbers and if it should add or subtract the numbers.

Specification

Given your level 2 program

When you enter 3 and 5

and you ask it to add the numbers

Then it will output 8


Given your level 2 program

When you enter 6 and 4

and you ask it to subtract the numbers

Then it will output 2

Level 3

For level three, create a program that will add, subtract, multiply and divide. Your program should ask for two numbers and the operation that you want it to perform.

Specification

Given your level 3 program

When you enter 3 and 5

and you ask it to multiply the numbers

Then it will output 15


Given your level 3 program

When you enter 12 and 4

and you ask it to divide the numbers

Then it will output 3

A note about specifications

Each level will contain one or more specifications. These specifications describe the level in a structured way. Each specification will have three parts starting with ‘given’, ‘when’, and ‘then’. Each of those parts has a specific purpose.

Given - This is where setup goes; it says what is expected to already exist.

When - Change happens here; this describes the action that is taken.

Then - Describes the results of the change.

The specification format comes from acceptance testing and behavior driven development.

Thank You For Your Participation

I hope that you enjoyed this challenge. If you are looking for additional CodeNewbie Challenges there is a list available on CodeNewbie Discourse.

Have an Idea for Challenge?

If you have an idea for a challenge, we would like to know about it. Please share your idea by posting it to the discourse thread.

Sharing your Solution

If you have created a solution to a challenge and have not yet shared it to CodeNewbie discourse, please do so. We have seen solutions in a variety of languages and styles and posted there and would love to see yours too.