Skip to main content

Exercises 8.15 Exercises

1.

Write the function summation which takes two parameters, start and end. summation adds all the integers from start to end, inclusive, together and returns the sum. Put the necessary blocks in the correct order.

2.

Write the function reverseNumber which takes num as a parameter and returns num but with its digits reversed. For example, reverseNumber (1324) returns 4231. Put the necessary blocks in the correct order, with reverse declared first, then temp, and lastly remainder.

3.

Write the function printStars. It that takes in an integer and should print * that many times, all on one line (no endls). If the function is passed 3 it should print ***. If it is passed 8 it should print ********. An argument of less than 1 should print no stars.
Includes and tests are present but hidden.
Hint.
You should focus on printing the right number of single *’s one at a time, not trying to figure out how to print a line of stars all at one time.

4.

The Collatz sequence is made by doing the following to a number until it becomes 1:
  • If the number is even, divide it by 2
  • Otherwise, multiply it by 3 and add 1
(It is believed but not proven that all positive integers eventually reach 1 using this process.)
Write a function collatz that accepts an integer and prints the Collatz sequence for that number. Print a space after each number in the sequence, including the final 1.
Example: An input of 13 should make the sequence: 13 40 20 10 5 16 8 4 2 1
Includes and tests are present but hidden.
Hint.
Remember that to check if a number is even you can mod by 2 and check if value is 0: x % 2 == 0

5.

Write a function powerSum that takes a single integer X as a parameter; you can assume it will be greater than 1. Add up all the numbers from 1 to 1000 (inclusive) that can be made by raising X to a power 1 or greater. Return the sum.
For example:
  • The powers of 5 that are less than or equal to 1000 are 5, 25, 125, 625. So an input of 5 would return 780.
  • The powers of 10 that are less than or equal to 1000 are 10, 100, 1000. So an input of 10 would return 1110.
Includes and tests are present but hidden.

6.

Write the function printSquare that has an integer parameter size. Print a square of *’s that is size tall and wide. An input of 3 should produce:
***
***
***
An input of 5 should produce:
*****
*****
*****
*****
*****
Includes and tests are present but hidden.
Hint.
This problem is best solved with nested loops. First just worry about using a loop to print one row of *s. Then add a loop to repeat that row size number of times.

7.

Write the function printTriangle that has an integer parameter size. Print a triangle of *’s that is size tall and wide that looks like these examples. An input of 3 should produce:
*
**
***
An input of 5 should produce:
*
**
***
****
*****
Includes and tests are present but hidden.
Hint.
Start by writing a loop to print out the number of stars on each row, like:
1
2
3
Then make an inner loop to print that many stars.

8.

Write the function printTriangle2 that has an integer parameter size. Print a triangle of *’s that is size tall and wide that looks like these examples. An input of 3 should produce:
  *
 ***
*****
An input of 5 should produce:
    *
   ***
  *****
 *******
*********
Includes and tests are present but hidden.
Hint 1.
Start by writing a single loop to print out the number of stars on each row, like:
1
3
5...
You can either calculate that number from the loop counter or make a starCount variable to keep track of the value.
Then add an inner loop to print the stars. Don’t worry about the spaces yet...
Hint 2.
The easiest way to solve this is with an outer loop and two inner loops. One inner loop will print the spaces, the next inner loop prints the stars:
for each row
    print spaces
    print stars
Hint 3.
How does the number of spaces relate to the size and the row number? If the size is 5, how many spaces are there at the start of row 1? How many at the start of row 2?
First worry about just printing the right number of spaces that should be there, like:
4*
3***
2*****
...
Then use
You have attempted of activities on this page.