4.6. Exercises#

Many of these exercises are taken from past exams of APS105 Computer Fundamentals courses at University of Toronto. The solutions are provided in the answer boxes.

Headings in this page classify the exercises into different categories: [Easy], [Intermediate], and [Challenging]. I suggest you start by easy exercises and work your way up to the challenging ones.

4.6.1. Loops#

Question 1 in Winter 2022 Final Exam [Easy]

Re-write the following code snippet replacing the for loop with a while loop. The changes you make should not change the functionality of the code snippet. Your code snippet should have exactly the same number of variables and the same variable names.

int result = 2;
for (int i = 2; i <= 989; i = i * 2) {
  result *= i;

Question 8 in Winter 2022 Final Exam [Easy]

Write a complete C program that calculates and prints the sum of all numbers between \(1\) and \(999\) (inclusive) that satisfy all the following conditions:

  • The number is divisible by \(9\).

  • The number is even.

  • The ten’s digit of the number is not \(7\). For example, the ten’s digit in \(753\) is \(5\), the ten’s digit of \(671\) is \(7\), and the ten’s digit of \(9\) is \(0\).

Modified version of Question 4 in Winter 2018 Midterm Exam [Intermediate]

Write a C program that prints the most significant digit of a positive int-type integer that is taken from the user. The program should prompt the user “Enter a number:” and prints “The leading digit is: ” followed by the most significant digit.

For example, if the user input is 987654321, the program will print 9. You can assume that the user always enters a positive integer.

Question 6 in Winter 2020 Midterm Exam [Intermediate]

The value of the mathematical constant \(e\) can be expressed using the infinite series: \( e = 1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + ...\)

Write a C program that approximates the value of \(e\) by approximating \(1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + ...\).

Rather than adding an infinite number of terms, your program should continue adding terms until the value of a term is less than \(0.001\). Your program should print the approximation to \(e\) and the number of terms used to determine the approximation. The terms in the series are \(1\), \(\frac{1}{1!}\), \(\frac{1}{2!}\) and so on.

Question 11 in Winter 2018 Midterm Exam [Challenging]

Write a C program that returns a randomly generated prime number between \(1\) and a maximum int-type integer, maxRange (inclusive), which is provided as by the user as input, and is assumed to be greater than \(1\). A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. For example, \(2\), \(3\), \(5\), \(11\), and \(13\) are all prime numbers.

The program prompts the user “Enter the maximum range: ” to enter the maxRange value and prints “Random prime number generated is ” followed by the randomly generated prime number. You can assume that the user always enters a positive integer greater than \(1\). You are not allowed to use arrays or pointer variables in your implementation. For convenience, you do not need to seed the random number generator.

4.6.2. Debugging/Finding Errors#

Question 8 in Winter 2018 Midterm Exam[Intermediate]

Identify and correct all compile-time errors you find in the C program below. Compile-time errors are errors — not warnings — that the compiler will report when compiling the program. Each line may or may not contain compile-time errors, and there may be more than one error per line.

Code with compile-time errors

 1#include <stdio.h>
 2int main(void) {
 3  double a, b = 3.14;
 4  do {
 5    int i = 0;
 6    printf("Enter a positive integer for offset: \n");
 7    scanf("%d", &a);
 8 } while (i < 5 && (a < 100 || a > 1);
 9 int j;
10 for (j = 0, j < 3, j++) {
11    y = b * j % a;
12    printf("%d\n", y);
14 return 0;