Arithmetic and Data Types - Exercises

Chapter chap4 section 16

  1. Write a program similar to exercise 8 of the previous chapter only displaying the percentage correct to 2 decimal places.

  2. Run the program that adds 30000 to 30000 on your computer. What size are your ints?

  3. Write a program that reads in a temperature expressed in Celsius (Centigrade) and displays the equivalent temperature in degrees Fahrenheit.

  4. If the variable x is of type double or type float then adding a suitably small number to it will not change the actual value stored. Write a program to read in an integer, calculate its reciprocal and store it in a variable called "r". Calculate and display the value of

    (1000+r)-1000

    Display the value using a "g" or "e" conversion. Run your program for various input integers. How big does the integer have to be before adding the reciprocal has no effect ?

  5. Write and run a program to find out whether your computer's char's are signed or unsigned.

  6. Write a program to read in a four letter word and print it out backwards.

  7. Write a program to read in a three digit number and produce output like

     3 hundreds
     4 tens
     7 units
    

    for an input of 347. There are two ways of doing this. Can you think of both of them? Which do you think is the better?

  8. The digital root of an integer is calculated by adding up the individual digits of a number. Write a program to calculate this (for numbers of up to 4 digits). Is it true that the digital root of a number divisible by 3 is also divisible by 3?

    Does this still hold if the number is expressed in hexadecimal or octal notation? I.e. if you add up the hexadecimal or octal digits is the sum divisible by 3. Write a program to find out.

  9. The notes include a program that got into trouble printing out floating point numbers producing very long lines of digits. Try this on your system.