Home
Examples
Labs
Assignment
Marks
Midterm
Final Exam
Resources

Extra Programming

Note that many of these programming problems are not easy.  They are meant to challenge you.  It may not be immediately obvious where you should start.  Try to find a piece of the problem that you do know how to solve.  Begin by coding that, and work from there.

I will not answer questions about the extra programming exercises during class.  I will be happy to help you outside of class, or I will post solutions upon request.


  1. Triangle Checker

    Write a program that prompts the user to enter information about a triangle.  Ask the user to enter the length of each of the three sides.  Your program should determine if the triangle is equilateral, isosceles, or scalene.  If the triangle is isosceles or scalene, your program should check to see if it is a right angle triangle.  Remember that:
    • An equilateral triangle has three equal sides.
    • An isosceles triangle has two equal sides.
    • A scalene triangle has no equal sides.
    • A right angle triangle satisfies the Pythagorean theorem (a^2 + b^2 = c^2).



  2. The Day You Were Born

    Write a program to determine the day of the week that a person was born on.  Your program should prompt the user to enter the date of their birth.  The following steps can be used to calculate the day of the week for any date in the 20th century (ie, this program is not YK2 compliant).
    1. Divide the last two digits of the birth year by 4.  Make sure to use integer division.  We only want the whole number.
    2. Add the last two digits of the birth year to the result from the last step.
    3. Add the date.
    4. Add the month number (Jan = 1, Feb = 4, Mar = 4, Apr = 0, May = 2, June = 5, July = 0, Aug = 3, Sept = 6, Oct = 1, Nov = 4, Dec = 6).
    5. If the year is a leap year and the month is January or February, then subtract 1.
    6. Find the remainder when dividing by 7 (Sun = 1, Mon = 2, Tue = 3, Wed = 4, Thu = 5, Fri = 6, Sat = 0).



  3. Fraction Math

    Write a program that prompts the user for two fractions.  You should read in the numerator and the denominator of each.  Next, ask the user what operation they would like to perform (addition, subtraction, multiplication or division).  Carry out the desired operation on the fractions and output the result.  You can use the "/" to display fractions (e.g. 1 / 2).



  4. Factorial

    Write a program that prompts the user for an integer.  Your program should compute and output the factorial of the integer.

                The factorial for 6 is computed as :
                      6 x 5 x 4 x 3 x 2 x 1 = 720
                The factorial for 4 is computed as :
                      4 x 3 x 2 x 1 = 24




  5. Summing Selectively

    Write a program that prints the sum of all odd number from 1 to 99.   Modify the program so that it prints the sum of all numbers between 1 and 99 that are divisible by 5.




  6. Calculate the Value of Pi

    Write a program that calculates the value of Pi using the Leibniz method.  Leibniz determined the following formula for estimating Pi :

                Pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ...

    Calculate and output Pi by evaluating the first 200 terms of the formula.




  7. The Largest Number

    Write a program that continues to prompt the user to enter a positive integer until the user enters a "Q" for quit.  If the user enters something other than a "Q" or a number, the program should print an error message reminding the user of appropriate input (either an integer or the letter Q) before asking for the next number.  When the user finally enters a letter Q, the program should display the largest number that the user inputted during program execution.  Assume that if a String starts with a number (digit from 0 to 9) then the String represents a valid integer.




  8. Printing a Calendar

    Write a program that prompts the user for the year, and the day of the week that the year starts on.  Based on these two pieces of information, your program should generate a calendar for the year that the user entered.  Print the calendar to an output file called "xxxxCal" where xxxx is the year of the calendar.  Don't forget to handle leap years.
    • Years divisible by 4 are leap years unless they are also divisible by 100.
    • Years divisible by 100 are not leap years unless they are also divisible by 400.

      Sample Output:

               January 2002
               S   M   T   W   Th  F   S  
               ========================== 
                       1   2   3   4   5  
               6   7   8   9   10  11  12 
               13  14  15  16  17  18  19 
               20  21  22  23  24  25  26 
               27  28  29  30 
      
      
               February 2002 
               S   M   T   W   Th  F   S  
               ========================== 
                               1   2   3  
               4   5   6   7   8   9   10 
               11  12  13  14  15  16  17 
               18  19  20  21  22  23  24 
               25  26  27  28  
      
      
               March 2002
               S   M   T ...