# Visual Basic 2005 Guide

Performing Calculations

## Introduction

Computers are good at performing calculations. Remember that the operators, **+ - * /** are used for arithmetic in Visual Basic programs. There are a number of built-in functions to carry out useful bits of Maths. For a full list, explore the MSDN (help) that comes with Visual Basic. Some of the most useful are listed below,

Function | Returns |
---|---|

Math.Abs(x) | The number x without its sign |

Math.Ceiling(x) | Rounds x up to the nearest integer |

Math.Floor(x) | Rounds x down to the nearest integer |

Math.Max(x, y) | The largest of the values x and y |

Math.Min(x, y) | The smallest of the values x and y |

Math.Pow(x, p) | Raises x to the power p |

Math.Round(x, d) | Rounds the number x to d decimal places |

Math.Sqrt(x) | The square root of x |

## Programming Tasks

- Write a program into which the user can enter the two shorter sides of a right-angled triangle. Use Pythagoras' Theorem to find the length of the longest side.
- A temperature in degrees Celsius can be converted to degrees Fahrenheit using the formula F = (9/5) * C + 32. Write a program that allows the user to input a temperature in Celsius and output the equivalent in degrees Fahrenheit.

## Random Numbers

Computers cannot generate actual random numbers. The term **pseudorandom **is used to reflect the fact that the numbers appear random.

In order to ensure that the 'random' numbers are not predictable in a program, we use the **RANDOMIZE **function at the beginning of the program. Random numbers are generated from a seed value. The randomize function changes the seed value for random numbers based on the current value of the computer's clock.

The **rnd()** function returns a random number less than 1 and greater than 0. To get a random integer between **a** and **b** where a is the smaller of the two numbers, you write,

`intRandom = CInt(Int((b - a + 1) * Rnd() + a))`

So, to get a random integer between 1 and 100, you would write,

`intRandom = CInt(Int((100 * Rnd()) + 1))`

## Another Programming Task

- Write a program that simulates the rolling of two dice.