Small Steps I — Creating with Text and Numbers

Storing values with fractional parts

Store a running time of 12.5 in a variable named seconds and print it.

What you will learn

Lengths and times may need fractional parts. double is a type for these numbers. Choose int for whole-number counts and double for values that need a fractional part.

Try it

Example · example.cpp
void small_main()
{
    double height = 1.45;
    print(height);
}

A variable's type is chosen when it is created. Assigning a fractional number to an int later does not turn it into a double variable.

double cannot store every number with unlimited precision, but for now we will start with simple lengths and times.

Expected output:

1.45

Exercise

Store a running time of 12.5 in a variable named seconds and print it.

Exercise starter · exercise_starter.cpp
void small_main()
{
    double height = 1.45;
    print(height);
}

Hint

Change the parts of the example that you need.

show solution
void small_main()
{
    double seconds = 12.5;
    print(seconds);
}
← Previous lessonNext lesson →