# The WHILE-statement

• Goal of Lab

In this lab we will learn to use the while statement.

• Relevant Material: Chapter 7 in book

• Preparation

```   mkdir ~/cs170/lab8                     # make lab directory
cd /home/cs170001/share/labs/lab8      # Go to share folder with lab8
cp * ~/cs170/lab8                      # Make a copy to your folder
cd ~/cs170/lab8                        # Go back to your lab folder
```

• If you use BlueJ

• If you are using BlueJ for the lab, you must open lab8 as "Non BlueJ"

I believe that you have work enough with BlueJ to turn Non-BlueJ Java projects into a BlueJ one.

• Lab Assignment

Edit the program file GCD.java in your ~/cs170/lab8 directory.

Your task is to write the following program in the GCD.java file. The program first reads in 2 integer numbers a and b. Then the program computes and prints the Greastest Common Divider (GCD) of the numbers a and b.

The GCD of a and b is the largest integer n, such that:

```        a / n  has a remainder 0
```
AND
```        b / n  has a remainder 0
```

• More details...

The GCD cannot be computed with some Mathematical formula, but you need to search and find it.

The number of integers is infinite, so you need to limit the range to search.

Use these facts to limit the range:

• The GCD of a and b must be smaller than either one of a or b.

• You program try every number (starting from 1 until the largest one possible) in the possible range and test if it is a common divider.

A common divider is a number n, such that:

```        a / n  has a remainder 0
```
AND
```        b / n  has a remainder 0
```

• Don't forget that you can perform AND, OR and NOT operation of logic values:

• the AND operator is: &&
• the OR operator is: ||
• the NOT operator is: !

• Finding the greatest common divider can be achieved by always keeping the larger common divider if you find a new one.

• Hint

• Try every number starting from 1 upto one of the input numbers (any one of the input numbers will do)

• The program must remember (save it in a variable - memory !) the last number that divides both input numbers

• After you try every number, print out the last number that you have recorded.

• Test

• Here are some GCD's:
```	Enter a: 36
Enter b: 48
The GCD of 36 and 48 is 12

Enter a: 96
Enter b: 72
The GCD of 96 and 72 is 24
```

• Turnin

• Turn in the lab before 6:00 PM with the following commands:
```   cd ~/cs170/lab8

/home/cs170001/turnin GCD.java  lab8

```