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

  • Finally, you can manage your Google Docs, uploads, and email attachments (plus Dropbox and Slack files) in one convenient place. Claim a free account, and in less than 2 minutes, Dokkio (from the makers of PBworks) can automatically organize your content for you.


Exercises - Converting Reals and Integer Values

Page history last edited by Steve Sweeney 12 years ago

Save these exercises as "14_exercise_1.t", "14_exercise_2.t", etc.


  1. Input two positive integers and perform integer and real division then output each for comparison. (solution)

    Sample Output:

    Please enter two positive integers:  8 3

    Integer Division:  8 / 3 = 2 remainder 2

    Real Division: 8 / 3 = 2.666667

    (a) Division by zero can cause programs to crash.  Modify your program to handle this condition.

    (b) Allow any integers, positive or negative.

    (c) Loop your program and have it stop on a specified condition.  Make sure you specify the condition to the user.


  2. Input two integers and determine if the smaller is evenly divisible into the larger.  The order that they are entered should not matter.  Division by zero can produce an error.  Any time your programs use division from now on, you need to check for division by zero. (solution)

    Sample Output:

    Please enter two integers:  8 3

    8 is NOT  divisible by 3


  3. Input an improper fraction (i.e., numerator is larger than denominator) and convert it to a mixed fraction (a whole number (quotient) and a proper fraction).  Use a loop to keep performing this operation until the user enters a proper fraction (i.e., stop when the numerator is less than the denominator). (solution)

    Sample Output:

    input numerator:  8

    input denominator: 3

    The improper fraction 8/3 can also be expressed as the mixed fraction 2 and 2/3

    input numerator:  5

    input denominator: 11

    Program Finished...

  4. Input an integer value and determine if it is a prime number.  A prime number is a number that is divisible only by one and itself.  For example, 11 is a prime number.  (solution)

    (a) Display all of the prime numbers between 1 and the number entered by the user. (solution)

    Sample Output

    Enter a positive integer greater than 1: 15

    15 is NOT a prime number

    Sample Output (extension a)

    Enter a positive integer greater than 1:  15

    The prime numbers between 1 and 15 are:  1, 2, 3, 5, 7, 11, 13


  5. Input an integer value and determine the smallest factors of that number.  (This is a very tough problem using only loops.  We can look at this again when we learn about recursion)

    Sample Output:

    Enter a positive integer greater than 1: 24

    24 = 2 x 2 x 2 x 3


  6. Write a program that reads a six-digit integer and prints the sum of the six digits.  Use integer division to extract each digit.  Loop.

    (a) Allow the user to enter up to 10 digits.  You will need to consider program efficiency or this program will seem very long and tedious to program.  Think of another loop.

    Sample Output:

    Enter a six-digit number:  123456

    The sum of your digits is 1 + 2 + 3 + 4 + 5 + 6 = 21


  7. Consider the following math problem:  How many four-digit positive integers (x) are there with the property that x and 3x have only even digits?  For example, one such number is x=8002, since 3x=24006 and each of the digits in x and 3x is even (divisible by 2). 

Comments (0)

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