BaseConvert - base = scan.nextInt(); // Compute the maximum...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// ************************************************ // BaseConvert.java // // Converts base 10 numbers to another base // (at most 4 digits in the other base). The // base 10 number and the base are input. // ************************************************ import java.util.Scanner; public class BaseConvert { public static void main (String[] args) { int base; // the new base int base10Num; // the number in base 10 int maxNumber; // the maximum number that will fit // in 4 digits in the new base int place0; // digit in the 1's (base^0) place int place1; // digit in the base^1 place int place2; // digit in the base^2 place int place3; // digit in the base^3 place int quotient; // quotient when dividing by the base Scanner scan = new Scanner(System.in); String baseBNum = new String (""); // the number in the new base // read in the base 10 number and the base System.out.println(); System.out.println ("Base Conversion Program"); System.out.println(); System.out.print ("Please enter a base (2 - 9): ");
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: base = scan.nextInt(); // Compute the maximum base 10 number that will fit in 4 digits // in the new base and tell the user what range the number they // want to convert must be in maxNumber = base * base * base * base - 1; System.out.println ("The maximum 4-digit number in base " + base + " is " + maxNumber + "."); System.out.print ("Please enter a base 10 number in the range 0 to " + maxNumber + " to convert: "); base10Num = scan.nextInt(); // Do the conversion (see notes in lab) place0 = base10Num % base; quotient = base10Num / base; place1 = quotient % base; quotient = quotient / base; place2 = quotient % base; quotient = quotient / base; place3 = quotient % base; quotient = quotient / base; // Print the result (see notes in lab) baseBNum = baseBNum + place3 + place2 + place1 + place0; System.out.println (base10Num + " (base 10) = " + baseBNum + " (base " + base + ")"); } }...
View Full Document

This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.

Page1 / 2

BaseConvert - base = scan.nextInt(); // Compute the maximum...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online