# 6.14. 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 `printMultTable`

function
to print out the triangular shape.

Loading a dynamic question ...

Selecting from: cp_6_AC_2q, cp_6_AC_2q_pp

Write a function called `printPyramid`

that prints out an `n`

x``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.

Loading a dynamic question ...

Selecting from: cp_6_AC_4q, cp_6_AC_4q_pp

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!

Loading a dynamic question ...

Selecting from: cp_6_AC_6q, cp_6_AC_6q_pp

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!

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.

Loading a dynamic question ...

Selecting from: cp_6_AC_8q, cp_6_AC_8q_pp

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 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.

Loading a dynamic question ...

Selecting from: cp_6_AC_10q, cp_6_AC_10q_pp