Activecode Exercises

Answer the following Activecode questions to assess what you have learned in this chapter.

Write a program that prints out the even numbers between 20 and 40, inclusive.

Below is one way to write the program

Write a program that counts down from 100 to 0 in decrements of 10.

Below is one way to write the program

Write a program that finds the sum of the first 10 natural numbers.

Below is one way to write the program.

Write a function, repreatHello, that is a void function that takes no arguments and uses a while loop to print out “hello” three times.

Below is one way to write the function

Now let’s generalize the repeatHello function so that it repeats a given string three times. Let’s write the code for the repeatString function, which takes input as a parameter and uses a while loop to print out the string three times.

Below is one way to write the function.

We can further generalize repeatString so that it repeats a given string a given number of times. Let’s write the code for the new repeatString function, which takes input and x as parameters and uses a while loop to print out the string x number of times.

Below is one way to write the function

On the last day of every year, we count down the seconds before the new year arrives. Write the function newYearCountdown, which prints out a countdown from 10 and then prints out “Happy New Year!”.

Below is one way to write the function.

Help Goku reach power levels of over 9000! Write the function powerUp which takes powerLevel as a parameter. powerUp checks to see if powerLevel is over 9000. If it isn’t, it repeatedly prints “More power!” and increments powerLevel by 1000 until powerLevel is over 9000. Then powerUp prints “It’s over 9000!”. Write the necessary code for the powerUp function.

Below is one way to write the function

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. Write the necessary code for the summation function.

Below is one way to write the function

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

Below is one way to write the function

You have attempted of activities on this page