Small Steps I — Creating with Text and Numbers

Making a small calculator

Read a width and height and print their area. Entering 3.5 and 2 gives 7.

What you will learn

input_real reads a number with a fractional part as a double. Connecting input → calculation → output makes a calculator.

Try it

Example · example.cpp
void small_main()
{
    double a = input_real();
    double b = input_real();
    print(a + b);
}

Type 2.5 and press Enter, then type 3.5 and press Enter. The function is called twice, so you enter two values. The program stores them in a and b and prints their sum.

Expected output (excluding input prompts):

6

Exercise

Read a width and height and print their area. Entering 3.5 and 2 gives 7.

Exercise starter · exercise_starter.cpp
void small_main()
{
    double a = input_real();
    double b = input_real();
    print(a + b);
}

Hint

Change the parts of the example that you need.

show solution
void small_main()
{
    double width = input_real();
    double height = input_real();
    print(width * height);
}
← Previous lessonNext lesson →