# Classwork 10: if Statements and while loops

## Objectives

To practice using if statements and a simple while loop.

## The Assignment

For this assignment, you may work in a team of 2 people if you would like to. To do this, submit using one person's account. The other person should submit a file that says who they worked with (include both real name and user name). One person should be the "driver" for Part 1. The driver is the person who has the keyboard and does the typing. (Other person watches out for bugs.) For Part 2, swap roles.

### Part 1

Write a program that prompts the user for an integer input Your program then informs the user whether the number is a square. If it is a square, your program should report its integer square root. A sample run of your program might look like (the user input is in orange):

```PT[235]% gcc -Wall is_square.c

PT[236]% ./a.out
n? 5
No, 5 is not a square.

PT[237]% ./a.out
n? 17
No, 17 is not a square.

PT[238]% ./a.out
n? 9
Yes, 9 is a square. It is the square of 3.

PT[239]% ./a.out
n? 1024
Yes, 1024 is a square. It is the square of 32.

PT[240]% ./a.out
n? 2048
No, 2048 is not a square.

PT[241]% ./a.out
n? 4096
Yes, 4096 is a square. It is the square of 64.
PT[242]%
```

You can check whether a number is a square with the help of the square root function. To do this, store the square root in an integer variable:

```r = sqrt(n) ;
```

Then check if r * r is equal to the user's input. Since you are using the sqrt() function, don't forget to

```#include <math.h>
```

and to compile your program with the -lm option.

```gcc -Wall is_square.c -lm
```

Use the script command to record some sample runs of your program and submit your program and typescript file:

```submit cs104_chang cw10 is_square.c
submit cs104_chang cw10 typescript
```

### Part 2

Write a program that uses a while loop to iterate through the numbers between 1 and 2000. For each of these numbers, check whether the number is a square. (You can reuse some of the code from Part 1, but this part does not have any user input.) If the number is indeed a square, print out a message. (No, this is not the most efficient way to list the squares between 1 and 2000. We're just practicing programming with a while loop.)

A sample run of your program might look like:

```PT[257]% gcc -Wall list_squares.c -lm

PT[258]% ./a.out
1 == 1 * 1
4 == 2 * 2
9 == 3 * 3
16 == 4 * 4
25 == 5 * 5
36 == 6 * 6
49 == 7 * 7
64 == 8 * 8
81 == 9 * 9
100 == 10 * 10
121 == 11 * 11
144 == 12 * 12
169 == 13 * 13
196 == 14 * 14
225 == 15 * 15
256 == 16 * 16
289 == 17 * 17
324 == 18 * 18
361 == 19 * 19
400 == 20 * 20
441 == 21 * 21
484 == 22 * 22
529 == 23 * 23
576 == 24 * 24
625 == 25 * 25
676 == 26 * 26
729 == 27 * 27
784 == 28 * 28
841 == 29 * 29
900 == 30 * 30
961 == 31 * 31
1024 == 32 * 32
1089 == 33 * 33
1156 == 34 * 34
1225 == 35 * 35
1296 == 36 * 36
1369 == 37 * 37
1444 == 38 * 38
1521 == 39 * 39
1600 == 40 * 40
1681 == 41 * 41
1764 == 42 * 42
1849 == 43 * 43
1936 == 44 * 44
PT[259]%
```

Use the script command to record a sample run of your program. Use the -a option to record your sample run to a new file:

```script -a typescript2
```

Don't forget to exit from script. Then, submit your program and sample run using.

```submit cs104_chang cw10 list_squares.c
submit cs104_chang cw10 typescript2
```

### Part 3 (optional)

If you are all done, consider these optional embellishments to your program:

• In Part 1, check if the user typed in a negative number. If so, tell the user that negative numbers cannot be squares.

• In Part 2, prompt the user to type in a beginning number and an ending number and list the squares between the beginning and the end.

Be sure to logout completely when you have finished!