5.15. Coding Practice

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

*
**
***
****
*****

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.

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:

*
**
***
****

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.

    *
   ***
  *****
 *******
*********

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.

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

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

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!

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

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!

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.

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

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.

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.

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!

You have attempted of activities on this page