Type a number ? 1.2 2
The variable DATA%
can only hold an integer,
which can not have a decimal point.
The user typed 1.2, but only the 1 was put into the
variable.
(The 1.2 is said to have been truncated to 1).
The 1 was then doubled, and 2 was output.
For these notes, programs will mostly use ordinary variables
like NUMBER
that do not have a special character
at the end of their name.
These variables can hold a useful range of floating point numbers.
If you enter a number like "1" (which you might think is an integer)
the number is stored in the variable as "1.0" which is really the same thing.
This avoids problems like the above.
Usually a program is written to solve a problem dealing with things in the world. Numbers are input to the program that describe real things. It is very useful to use variable names that describe what the numbers mean.
Here is a program that asks the user for the number of fluid ounces and calculates the number of gallons. Notice how the names of the variables make it much easier to see what is going on:
' Convert Fluid ounces to Gallons ' PRINT "Enter number of fluid ounces" INPUT OUNCES PRINT "Number of gallons:", OUNCES / 128 END
Write a program that asks the user for the number of grams and prints the number of kilograms. There are 1000 grams per kilogram. Use the above program as a model.
(Write this program with paper and pencil, or just "think" it. The best thing to do would be to use QBasic to write the program and run it before you go on.)