## Chapter II

#### Mathematical Calculations

QBasic was obviously created for us to have fun, play games, draw nice graphics and even
make sounds. But, as you might guess, nothing good comes without a bit of effort that has
to be put in it. In the most QBasic programs a bit of math has to be done.
The math… Doh!
If you hate mathematics, don’t worry. Qbasic will do it all for you, you just need to know
how to tell QBasic to do that.

Qbasic can perform the following mathematical operations:

Operator | What it does | Example | Result |
---|---|---|---|

+ | Add | 7 + 2 | 9 |

- | Subtract | 7 - 2 | 5 |

* | Multiply | 7 * 2 | 14 |

/ | Divide | 7 / 2 | 3.5 |

#### Example 1:

```
a = 15 / 4 + 3
```

PRINT a

Result on the screen:

`6`

#### Example 2:

```
PRINT "Enter the first number"
```

INPUT a

PRINT "Enter the second number"

INPUT b

c = a + b

d = a * b

PRINT a; "+"; b; "="; c

PRINT a; "*"; b; "="; d

END

When you run this program it goes like this:

*Computer:*

`Enter the first number`

*You:*

`Enter the first number 22`

*Computer:*

`Enter the first number 22`

Enter the second number

Enter the second number

*You:*

`Enter the first number 22`

Enter the second number 18

Enter the second number 18

*Computer:*

`Enter the first number 22`

Enter the second number 18

22 + 18 = 40

22 * 18 = 396

Enter the second number 18

22 + 18 = 40

22 * 18 = 396

Advanced operations:

Operator | What it does | Example | Result |
---|---|---|---|

\ | divides and turns the result into an integer(the whole number) |
7 \ 2 | 3 |

^ | Raises a number to the power of another number |
3 ^ 4 (means: 3*3*3*3) 2.5 ^ 3 (means: 2.5*2.5*2.5) |
243 15.625 |

SQR | Calculates the square root of a number |
SQR(9) SQR(16) SQR(5) |
3 (because: 3^2=9) 4 (because: 4^2=16) 2.236 |

MOD | Divides two numbers, and if the result is not an integer (for example - 3.25), finds out how much to subtract from the first number in order to get the integer result. |
17 MOD 5 | 2 (because: 17 / 5 = 3.4 17 – 2 = 15 15 / 5 = 3) |