| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Programming Exercises - Integer Division and Modulo

Page history last edited by Steve Sweeney 8 years, 7 months ago

 

  1. Use the symbols +, -, *, /, div, mod, (, and ) together with the numbers 1, 6, 7, and 8 to create output expressions with values of 0, 1, 2, ..., 10.  You may use the symbols as many times as you like.  You may only use the numbers 1, 6, 7, 8 one time for each expression (and each number must be used).

    For example:  put (1 div 8) + (6 div 7) will output 0.

  2. Airport runways are given numbers determined by the direction in which planes travel as they move along the runways.  The number is found by taking the bearing in degrees and rounding to the nearest 10 degrees and then dropping the last zero.

    For example, a runway with a bearing of 267.5 would:
    (a) round 267.5 to 270.
    (b) discard last zero, to 27

    Write a program to read a bearing (from 0 to 360) and output the corresponding runway number.  Make sure you make appropriate use of variables for your calculations.

  3. Write a program that prompts the user for a 4-digit, positive integer value (e.g., from 1111 to 9999).  Use integer division by 1000 to isolate the first digit.  Use the remainder to isolate the remaining three digits.  Repeat this process using division by 100, 10, and 1 to isolate the remaining digits.  The goal of the program is to output the list of digits.

    For example:
    Please enter a positive, 4-digit number (1111 to 9999): 2358
    The digits of 2358 are 2, 3, 5, and 8

  4. When performing calculations, it is often required to round our answer to a specific number of decimal places.  Many programming languages provide this ability through their output commands, but it is possible to accomplish this using multiplication, division, and the mathematical "round" command.

    Example 1:  Round the value 2.785 to one decimal place.
    - for one decimal place, multiply the number by 10 (or 10 to the power 1):  2.785 * 10 is 27.85
    - round the new value:  27.85 rounds to 28
    - divide the rounded value by 10:  28 / 10 is 2.8

    Example 2:  Round the value 3.14159 to three decimal places.
    - for three decimal places, multiply by 1000 (or 10 to the power 3):  3.14159 * 1000 is 3141.59
    - round the new value:  3141.59 rounds to 3142
    - divide by 1000:  3142 / 1000 is 3.142

    Write a program that will prompt the user for two values.  The first value is the number you wish to round (i.e., a number with decimals).  The second number is the number of decimal places (0, 1, 2, ...), which is an integer.
    Example:  first number = 3.14159, second number = 3, output = 3.142

  5. Modify your rounding program so that it can round to any digit (i.e., not just decimals, but also the tens digit, or hundreds digit, etc.).  How will you handle the input to tell the difference between rounding to the left or right of the decimal place?  Your previous program may actually already do this, but you need to understand why.  This question is more about math understanding than programming.

  6. Fractions are represented as an integer numerator over an integer denominator.  There are three types of fractions:  proper fractions (numerator less than denominator), improper fractions (numerator greater than denominator), and mixed fractions (a whole number combined with a proper fraction).

    To change an improper fraction into a mixed fraction, you can use integer division and the remainder operator (modulo).  Write a program that will allow the user to enter two integer values, a numerator and denominator.  The program should output these values as an improper fraction, and then output the equivalent mixed fraction.

    For example:
    numerator? 8 
    denominator? 3
    8 / 3 is equivalent to 2 and 2 / 3

  7. Modulo can be useful for controlling an event every Nth repetition of a loop.  For example, during a counted loop from 1 to 100, you may want to have something happen every 10th pass through the loop.  Rather than build in a check for i = 10, 20, 30, etc., you can check to see if modulo 10 is zero.

    (a) Some programs take a long time to process their information, and the user may wonder if the program is still running.  Write a program with a long loop (say 1000), and use modulo to output a period character (on the same line) every 50 iterations of the loop.
    (b) Write a program which generates random numbers between 1 and 100 and outputs them to the screen, on the same line.  To improve formatting, use modulo to change to a new line every 10 numbers printed.  Extend this program by allowing the user to specify how many numbers per line.

  8. Write a program to accept an integer number from the user, and determine if that number is even.
    (a) Extend this by making a subprogram, "isEven()", which takes an integer parameter and returns TRUE or FALSE, depending on whether the value is even or odd.

  9. Write a program to accept an integer number from the user, and determine if that number is prime.
    (a) Extend this by making a subprogram, "isPrime()", which takes an integer parameter and returns TRUE or FALSE, depending on whether or not the number is prime.

  10. Write a program that uses modulo and integer division to make change using coins: $2, $1, $0.25, $0.10, $0.05, and $0.01.

  11. Write a program that will print out the numbers from 1 to N, using M numbers per line, where the user decides both N and M.  For example:
    Print from 1 to ? 20
    How many per line? 8
    1 2 3 4 5 6 7 8
    9 10 11 12 13 14 15 16
    17 18 19 20

Comments (0)

You don't have permission to comment on this page.