Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Exercises 6.14 Coding Practice

1.

Write a program that prints out a 5x5 triangle using asterisks. An example is shown below. Your code should use while loops.
*
**
***
****
*****
Solution.
Below is one way to implement the program. We use nested loops similar to the last version of the printMultTable function to print out the triangular shape.
#include <iostream>
using namespace std;

int main() {
    int row = 0;
    while (row < 5) {
        int col = 0;
        while (col <= row) {
            cout << "*";
            col++;
        }
        cout << endl;
        row++;
    }
}

2.

Encapsulate the triangle printing program into a function called printTriangle. Generalize it so that it takes a parameter int n to generate a nxn triangle. Check the hint below for help with the construction of the code. Call your function in main with an input of 4, which should result in the following output:
*
**
***
****
Hint.

Activity 6.14.1.

Encapsulate the triangle printing program into a function called printTriangle. Generalize it so that it takes a parameter int n to generate a nxn triangle. Use the lines to construct the code, then go back to complete the Activecode. Call your function in main with an input of 4, which should result in the following output:
*
**
***
****

3.

Write a function called printPyramid that prints out an n by n pyramid using asterisks. An example is shown below with n equal to 5. Your code should use while loops.
*
**
***
****
*****
Solution.
Below is one way to implement the program. We use multiple while loops to print out spaces and asterisks. The outer loop creates the number of rows, and within the outer loop, the two inner loops print out the correct number of spaces and asterisks.
#include <iostream>
using namespace std;

void printPyramid(int n) {
    int space, numAsterisks;
    int count = 1;
    while (count <= n) {
        space = n - count;
        while (space > 0) {
            cout << " ";
            space--;
        }
        numAsterisks = 2 * count - 1;
        while (numAsterisks > 0) {
            cout << "*";
            numAsterisks--;
        }
        cout << endl;
        count++;
    }
}

int main() {
    printPyramid (5);
}

4.

Write a function called printNumPyramid that prints out an n x n number pyramid. An example is shown below with n equal to 5. Your code should use while loops. Check the hint below for help with the construction of the code. (Hint: similar to the previous question, if you want the output to look nice, using conditionals that print different amounts of spaces.)
    1
   222
  33333
 4444444
555555555
Hint.

Activity 6.14.2.

Write a function called printNumPyramid that prints out an n x n number pyramid. An example is shown below with n equal to 5. Your code should use while loops. Use the lines to construct the code, then go back to complete the Activecode. (Hint: similar to the previous question, if you want the output to look nice, using conditionals that print different amounts of spaces.)
    1
   222
  33333
 4444444
555555555

5.

A common coding interview question that’s also a popular children’s game used to teach division is FizzBuzz.
Write a program that uses a while loop and prints the numbers 1 through 100, but every multiple of 3 is replaced with the word "Fizz" every multiple of 5 is replaced with the word "Buzz" and every multiple of both 3 and 5 is replaced with "FizzBuzz." Your output should be the following:
1
2
Fizz
4
Buzz
...
14
FizzBuzz
16
...
98
Fizz
Buzz
Solution.
Below is one way to implement the “FizzBuzz” program. We use conditionals with modulus operators in a while loop to categorize every number and print the correct output. Feel free to search up on the FizzBuzz coding interview problem if you are interested in other ways to code this program!
#include <iostream>
using namespace std;

int main() {
    int n = 1;
    while (n <= 100) {
        if (n % 3 == 0 && n % 5 == 0) {
            cout << "FizzBuzz" << endl;
        }
        else if (n % 3 == 0) {
            cout << "Fizz" << endl;
        }
        else if (n % 5 == 0) {
            cout << "Buzz" << endl;
        }
        else {
            cout << n << endl;
        }
        n++;
    }
}

6.

Write the function printAddTable which takes an int n as a parameter and prints out a nxn addition table. Call your function in main with “10” as the argument. Check the hint below for help with the construction of the code. Your output should look like this:
0       1       2       3       4       5       6       7       8       9       10
1       2       3       4       5       6       7       8       9       10      11
2       3       4       5       6       7       8       9       10      11      12
3       4       5       6       7       8       9       10      11      12      13
4       5       6       7       8       9       10      11      12      13      14
5       6       7       8       9       10      11      12      13      14      15
6       7       8       9       10      11      12      13      14      15      16
7       8       9       10      11      12      13      14      15      16      17
8       9       10      11      12      13      14      15      16      17      18
9       10      11      12      13      14      15      16      17      18      19
10      11      12      13      14      15      16      17      18      19      20
Hint.

Activity 6.14.3.

Write the function printAddTable which takes an int n as a parameter and prints out a nxn addition table. Call your function in main with “10” as the argument. Use the lines to construct the code, then go back to complete the Activecode. Your output should look like this:
0       1       2       3       4       5       6       7       8       9       10
1       2       3       4       5       6       7       8       9       10      11
2       3       4       5       6       7       8       9       10      11      12
3       4       5       6       7       8       9       10      11      12      13
4       5       6       7       8       9       10      11      12      13      14
5       6       7       8       9       10      11      12      13      14      15
6       7       8       9       10      11      12      13      14      15      16
7       8       9       10      11      12      13      14      15      16      17
8       9       10      11      12      13      14      15      16      17      18
9       10      11      12      13      14      15      16      17      18      19
10      11      12      13      14      15      16      17      18      19      20

7.

A number is a prime number if its only factors are 1 and itself.
Write the function isPrime, which takes an int num as a parameters. isPrime is a boolean function that returns true if num is a prime number and returns false otherwise. Run and test your code!
Solution.
Below is one way to implement the isPrime function. First, we check to see if num is less than or equal to 1, and return false if that is the case. Next, we use a while loop to continuously check if a factor n divides num evenly. If it does, we return false. If no value of n divides num evenly, then we return true. Notice the while loop only goes up to num / 2 because if 2 doesn’t divide evenly, then there isn’t a smaller factor.
#include <iostream>
using namespace std;

bool isPrime(int num) {
    if (num <= 1) {
        return false;
    }
    int n = 2;
    while (n < num / 2) {
        if (num % n == 0) {
            return false;
        }
        n++;
    }
    return true;
}

8.

Write a program that uses a while loop to print out the alphabet from a to z. Check the hint below for help with the construction of the code.
Hint.

Activity 6.14.4.

Write a program that uses a while loop to print out the alphabet from ‘a’ to ‘z’. Use the lines to construct the code, then go back to complete the Activecode.

9.

The Fibonacci sequence is a sequence of numbers such that each successive number is the sum of the two previous numbers. This sequence is as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.
Write a program that prints the first 20 Fibonacci numbers.
Solution.
Below is one way to implement the program. First, we check to see if num is less than or equal to 1, and return false if that is the case. Next, we use a while loop to continuously check if a factor n divides num evenly. If it does, we return false. If no value of n divides num evenly, then we return true. Notice the while loop only goes up to num / 2 because if 2 doesn’t divide evenly, then there isn’t a smaller factor.
#include <iostream>
using namespace std;

int main() {
    int first = 0;
    int second = 1;
    int third;
    int n = 2;
    cout << first << " " << second << " ";
    while (n < 20) {
        third = first + second;
        cout << third << " ";
        first = second;
        second = third;
        n++;
    }
}

10.

Write a function called factorial which takes an int n as a parameter and returns n factorial. Remembers that a factorial(denoted by !) is the product of all positive integers less than or equal to n, so 4! is 24. Use a while loop. Run and test your code! Check the hint below for help with the construction of the code.
Hint.

Activity 6.14.5.

Write a function called factorial which takes an int n as a parameter and returns n factorial. Remembers that a factorial(denoted by !) is the product of all positive integers less than or equal to n, so 4! is 24. Use a while loop. Run and test your code! Use the lines to construct the code, then go back to complete the Activecode.
You have attempted 1 of 16 activities on this page.