CSCI 220
HOMEWORK ASSIGNMENT #3
Assigned Date: Tuesday, September 23, 2003
Due Date: Friday, October 3, 2003
Due Time: Noon

 

Purpose:

This assignment focuses on iteration (sentinel and count-controlled loops), selection statements, and error checking.

Source filename to be submitted:  DecimalToBinary.java

 

Documentation:

See instructions in the first homework assignment.

 

Assignment: 

Write a program to convert a decimal number to the equivalent binary representation. 

Here is a sketch of the algorithm:  Suppose we wish to convert 53 to binary.  We begin by finding the largest power of two (2x) that is less than or equal to 53 (i.e., x is 5).  Then we divide 53 by that power of two (i.e., 25).  The integer quotient of this division is 1, which is the leftmost digit of the binary equivalent.  Next, we do this again with the remainder of the previous division (i.e., 21) and the next smaller power of two (i.e., 24).  This produces the next digit of the binary equivalent (i.e., 1).  We repeat this process until 20.

 

Sample Run 1: (user input in italics)

Enter a decimal number (non-negative): 53

The equivalent binary representation is 110101.

 

Sample Run 2: (user input in italics)

Enter a decimal number (non-negative): -3

Sorry, I do not know how to convert negative numbers to binary.

 

Notes:

  1. Follow the above I/O format carefully.
  2. Test your program with different inputs to ensure that it works properly.
  3. The above algorithm can be used to convert decimal numbers to other number systems.  For instance, for conversion to octal use power of eight; for conversion to hexadecimal, use power of 16 (and, of course, use symbols A, B, C, D, E, F for digits larger than 9).  Hint: Write you program so that it can be easily modified to work with other number systems.

 

Submission: 

See instructions in the first homework assignment.