1. (25 points) Write a program that reads a number num (an integer) from the keyboard and:

Prints all the numbers less than or equal to num that are prime numbers. A prime number is one that is divisible only by 1 and the number itself. When a number is divisible by another number there is no remainder.

You must use a helper function to determine if each number is prime. That means that you have to write a function that determines if a number is prime and use it in the program.

Sample run:

```   Please enter number: 7
Prime numbers <= 7: 1, 2, 3, 5, 7
```

Notes and Hints:

• Test whether the values of the loop control variable, index are divisors of num.
• What will be the upper limit of the loop ?
• How do you determine if a number is a divisor of num?

```   #include <stdio.h>

int IsPrime(int number);

int main()
{
int num = 0, index = 0;

scanf("%d", &num);

printf("\n");

/* We will print 1 right away: that's always true */

printf("Prime numbers <= %d: 1 ", num);

/* Loop through all the numbers less than this and print the primes */

for(index = 2; index <= num; index++)
{
if( IsPrime(index) == 1 )
{
printf("%d ", index);
}
}

printf("\n");

return 0;
}

/* Function to determine if a number is prime */
/* Will return 1 if it is, 0 if not.          */

int IsPrime(int number)
{
int index = 0;

/* Test all numbers less than the one passed in */
/* Of course we don't test 1                    */

for(index = 2; index < number; index++)
{
if( (number % index) == 0 )
{
/* Found a divisor, return right away */

return 0;
}
}

/* If we got this far, it must be prime */

return 1;
}
```

2. (25 Points)
Write a program that reads a series of integers terminated by a 0. The program should then print:

• The number of integers
• The average (with 1 decimal digit of precision)
• The maximum number
• The minimum number
Example:
```   Enter a series of integers terminated by a 0.
80 90 75 66 82 92 0

You have entered 6 values.
The average is: 80.8
The maximum is: 92
The minimum is: 66
```

```   #include

int main()
{
int total = 0, entries = 0, value = 0, min = 0, max = 0;

printf("Enter a series of integers terminated by a 0\n");

/* Read first value and use that to initialize */

scanf("%d", &value);
min = value;
max = value;

while( value != 0 )
{
entries++;
total += value;
if( value < min )
{
min = value;
}
else
{
if( value > max )
{
max = value;
}
}

scanf("%d", &value);
}

printf("\n");
printf("You have entered %d values.\n", entries);
printf("The average is: %.1f\n", (float) total / entries);
printf("The maximum is: %d\n", max);
printf("The minimum is: %d\n", min);

return 0;
}
```

3. (20 Points) Write a function that prints a box on the screen. The arguments for the function are:

• The height of the box
• The width of the box
• The character used to draw the lines of the box.
The box must have dimension of at least 2x2 and no more than 40x40.
• The function returns 1 if either the height or the width are outside the allowable sizes
• return 0 otherwise.

```   int Box(int width, int height, char letter)
{
int index = 0, spaces = 0;

if( (width < 2) || (width > 40) || (height < 2) || (height > 40) )
{
return 1;
}

for(index = 0; index < width; index++)
{
printf("%c", letter);
}
printf("\n");

for(index = 0; index < (height-2); index++)
{
printf("%c", letter);
for(spaces = 0; spaces < (width-2); spaces++)
{
printf(" ");
}
printf("%c\n", letter);
}

for(index = 0; index < width; index++)
{
printf("%c", letter);
}
printf("\n");

return 0;
}
```

4. (15 Points)
The following code contains at least 5 errors (syntax and/or logic). Identify line number and error.

```   1.   #include <stdio.h>

3.   int DoCommand(char command, amount);

4.   int main()
5.   {
6.	   char inp;
7.	   int error;
8.	   float amount;
9.      while( error == 0 )
10.	   {
11.	      printf("Command: ");
12.	      inp = getchar();

13.	      if( (inp == 'E') && (inp == 'e') )
14.	         error = -1;
15.	      else
16.	      {
17.	         printf("Amount: ");
18.		 scanf("%f", &amount);
19.		 getchar();
20.	         error = DoCommand(inp, amount);
21.	      }
22.	   }
23.	   return 0;
24.	}

25.	int DoCommand(char command, float amount)
26.	{
27.	   int error = 0;
28.	   switch( command )
29.	   {
30.	      case 'A', 'a':
32.		 break;
33.	       default:
34.	         error = 1;
35.		 break;
36.     }
37.	  return error;
38.	}

40.	{
42.      return 0;
43.	}
```
 1 Line: 3 Error: function arguments must be preceeded by type. amount is not typed. 2 Line: 9 Error: use error without having initialized it. 3 Line: 10 Error: inp will never be equal to both 'E' and 'e' 4 Line: 30 Error: cannot put more than one value for case 5 Line: 31 Error: DoAdd expects an int parameter, amount is float

5. (15 Points) What does the following code produce ?

```   #include

void ShowTotal(int total);
int Subtraction(int total, int value);

int main()
{
int index = 0, total = 0;
for(index = 0; index < 10; index++)
{
if( index % 2 )
else
total = Subtraction(total, index);
}

return 0;
}

void ShowTotal(int total)
{
printf("Running total = %d\n", total);
}

{
ShowTotal(total);
}

int Subtraction(int total, int value)
{
ShowTotal(total);
}
```

```   Running total = 0
Running total = 0
Running total = 2
Running total = 0
Running total = 6
Running total = 2
Running total = 12
Running total = 6
Running total = 20
Running total = 12
```