Practice Programs

Here are some small-ish program for you to write as practice. The more you write, the better you will learn the class material.

One good way to practice is this: if you solve a problem with a for loop, try re-writing it with a while loop as well (and vice versa).

  1. Write a program to print the integers from 30 down to 1 in descending order. This can be done at least 2 different ways.

  2. Write a program to find how many multiples of 17 there are between 1 and 10000. This can be done at least 3 different ways.

  3. Write a program to print the multiples of 17 between 1 and 300.

  4. FizzBuzz: Write a program to that asks the user for an upper limit; it then prints each integer from 1 to the upper limit... EXCEPT for these changes: if the number is a multiple of 3, it prints "Fizz"; if the number is a multiple of 5, it prints "Buzz"; if the number is a both a multiple of 3 and 5, it prints "FizzBuzz".

  5. Write a program that asks the user to type a word. Then it will ask for another word, and tell whether the word just typed is ahead, or after, the user's previous word (using dictionary order). Keep on asking for new words, comparing it to the immediately prior word, until the user types "done".

  6. Write a program that asks the user to type a word. It then prints whether the word has an even or odd number of characters. Keep asking for new words until the user types "."

  7. Write a program that determines if a number the user supplies is prime or not. Write your own algorithm to determine primality.

  8. Write a program to build an array "munge" that has this property: for every index k in the array, munge[k] is k^3.

  9. Write a program to build an array "munge" that has this property: for every index k in the array, munge[k] is k*(k+1). Also compute the sum of all the elements in munge.

  10. Write a program to build an array "munge" that has this property: for every index k in the array, munge[k] is k^(k+1). Do this two ways: (1) use the Math library function to raise to a power; (2) compute the power yourself, by using a loop to multiply k by itself the right number of times (think accumulator).

  11. Write a program to find all primes between 1 and 200. Do this by the Sieve of Eratosthenes. The Sieve is a classic algorithm that dates to ancient Greece. The Sieve is a fairly efficient way to find primes up to a few million or so, but you certainly don't want to deal with a few hundred thousand "alert" popups getting your output.
    You can google up the details, but here are the basics. Make an array "prime" of boolean, big enough to run from item 0 to item 200. Start with all items in the array set to true. You can ignore item 0. Since 1 is prime and prime[1] is already true, you start with 2. prime[2] is already true (and 2 is prime) so go through the rest of the array and mark all multiples of 2 to false... all multiples of 2 are not prime. Then move to item index 3. If prime[3] is true (and it is) then 3 is prime and you then mark all multiples of 3 as false (not prime). And so on... until you hit the end of the array. Now every item in array prime is either true or false, depending on if the corresponding index is prime or not.

  12. Write a program that will read some number of strings from the user as input and store the strings into an array. Then sort the array elements (rearrange their order) so that the item at location 0 is alphabetically the smallest string and all the other are in ascending order. If you are googl'ing, you can look for "bubble sort". It is inefficient for large numbers of strings, but it is easy to program and works fine for smaller array sizes.

  13. Write a function "isPrime" that takes one argument, an integer, and returns a true or false. The obvious behavior is what you should program: isPrime(n) returns a true if n is a prime number, and returns a false if n is not prime. You have one version of this function... I gave it to you as part of the Assignment 3 template. Try to write it yourself. Don't worry about how efficient it is for now, just see if you can design an algorithm that will get the job done. You can test your function by also calling the one I gave you and seeing if they both produce the same results.

  14. Write a program that will generate 200 random integers into an array. The numbers should be in the range 0 to 1000. Then compute the average of the numbers in the array, and truncate that average to an integer. Then search through the array and see if that truncated average actually appears as one (or more) of the array elements. If you do find it, print the location (index) in the array where the number was found. Do this for all occurrances you find. Finally, count how many times you find it, and report that count.