5.15. Coding PracticeΒΆ

Q1

Question

Write a program that prints out a 5x5 triangle using asterisks. An example is shown below. Your code should use while loops.

*
**
***
****
*****
Example cp_6_AC_1q
1#include <iostream>
2
3int main() {
4    // Write your implementation here.
5}

Answer

Below is one way to implement the program. We use nested loops similar to the last version of the multiples_table function to print out the triangular shape.

Example cp_6_AC_1a
 1#include <iostream>
 2using std::cout;
 3
 4int main() {
 5    int row = 0;
 6    while (row < 5) {
 7        int col = 0;
 8        while (col <= row) {
 9            cout << '*';
10            col++;
11        }
12        cout << '\n';
13        row++;
14    }
15}

Q2

Encapsulate the triangle printing program into a function called print_triangle. Generalize it so that it takes a parameter int n to generate a nxn triangle. Call your function in main with an input of 4, which should result in the following output:

*
**
***
****
Example cp_6_AC_2q
1#include <iostream>
2
3void print_triangle (int n) {
4    // Write your implementation here.
5}
6
7int main() {
8    // Write your implementation here.
9}

Q3

Question

Write a function called print_pyramid that prints out an nx``n`` pyramid using asterisks. An example is shown below with n equal to 5. Your code should use while loops.

    *
   ***
  *****
 *******
*********
Example cp_6_AC_3q
1#include <iostream>
2
3void print_pyramid (int n) {
4    // Write your implementation here.
5}
6
7int main() {
8    print_pyramid (5);
9}

Answer

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.

Example cp_6_AC_3a
 1#include <iostream>
 2using std::cout;
 3
 4void print_pyramid(int n) {
 5    int count = 1;
 6    while (count <= n) {
 7        int space = n - count;
 8        while (space > 0) {
 9            cout << ' ';
10            space--;
11        }
12        int num_asterisks = 2 * count - 1;
13        while (num_asterisks > 0) {
14            cout << '*';
15            num_asterisks--;
16        }
17        cout << '\n';
18        count++;
19    }
20}
21
22int main() {
23    print_pyramid (5);
24}

Q4

Write a function called number_pyramid 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. (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
Example cp_6_AC_4q
1#include <iostream>
2
3void number_pyramid (int n) {
4    // Write your implementation here.
5}
6
7int main() {
8    number_pyramid (5);
9}

Q5

Question

A common coding interview question that's also a popular children's game used to teach division is fizz_buzz. 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 "fizz_buzz." Your output should be the following:

1
2
Fizz
4
Buzz
...
14
fizz_buzz
16
...
98
Fizz
Buzz
Example cp_6_AC_5q
1#include <iostream>
2
3int main() {
4    // Write your implementation here.
5}

Answer

Below is one way to implement the "fizz_buzz" 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 fizz_buzz coding interview problem if you are interested in other ways to code this program!

Example cp_6_AC_5a
 1#include <iostream>
 2
 3int main() {
 4    using std::cout;
 5    int n = 1;
 6    while (n <= 100) {
 7        if (n % 15 == 0) {
 8            cout << "fizz_buzz\n";
 9        }
10        else if (n % 3 == 0) {
11            cout << "Fizz\n";
12        }
13        else if (n % 5 == 0) {
14            cout << "Buzz\n";
15        }
16        else {
17            cout << n << '\n';
18        }
19        n++;
20    }
21}

Q6

Write the function addition_table 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. 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
Example cp_6_AC_6q
1#include <iostream>
2
3void addition_table (int n) {
4    // Write your implementation here.
5}
6
7int main() {
8    // Call your function here.
9}

Q7

Question

A number is a prime number if its only factors are 1 and itself. Write the function is_prime, which takes an int num as a parameters. is_prime is a boolean function that returns true if num is a prime number and returns false otherwise. Run and test your code!

Example cp_6_AC_7q
1bool is_prime (int num) {
2    // Write your implementation here.
3}

Answer

Below is one way to implement the is_prime 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.

Example cp_6_AC_7a
 1#include <iostream>
 2
 3bool is_prime (int num) {
 4    if (num <= 1) {
 5        return false;
 6    }
 7    int n = 2;
 8    while (n < num / 2) {
 9        if (num % n == 0) {
10            return false;
11        }
12        n++;
13    }
14    return true;
15}

Q8

Write a program that uses a while loop to print out the alphabet from 'a' to 'z'.

Example cp_6_AC_8q
1#include <iostream>
2
3int main() {
4    // Write your implementation here.
5}

Q9

Question

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.

Example cp_6_AC_9q
1#include <iostream>
2
3int main() {
4    // Write your implementation here.
5}

Answer

Below is one way to implement the program. First, we initialize the first two Fibonacci numbers and print the first two values out right away. Next we use a while loop to compute the remaining values. The next number in the sequence is the sum of the previous two, so we compute the sum and then we need to update the values of the "previous two Fibonacci numbers" for the next loop iteration.

Example cp_6_AC_9a
 1 #include <iostream>
 2
 3 int main() {
 4     int first = 0;
 5     int second = 1;
 6     std::cout << first << ' '
 7               << second << ' ';
 8     int n = 2;
 9     while (n < 20) {
10         int next = first + second;
11         std::cout << next << ' ';
12         first = second;
13         second = next;
14         ++n;
15     }
16 }

Q10

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!

Example cp_6_AC_10q
1#include <iostream>
2
3int factorial (int n) {
4    // Write your implementation here.
5}