Skip to content

Instantly share code, notes, and snippets.

@imryan
Last active December 24, 2015 22:19
Show Gist options
  • Save imryan/6871657 to your computer and use it in GitHub Desktop.
Save imryan/6871657 to your computer and use it in GitHub Desktop.
Base conversion.
import java.util.*;
public class LabGrade
{
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 placeholder;
String baseBNum = new String (""); // the number in the new base
Scanner scan = new Scanner(System.in);
// read in the base 10 number and the base
System.out.println ("\nBase Conversion Program");
System.out.println("Please enter a base (2-9):\n");
base = scan.nextInt();
// Compute the maximum base 10 number that will fit in 4 digits
maxNumber = (base * base * base * base) - 1; // b^4 - 1
System.out.println("Base: " + maxNumber);
// in the new base and tell the user what range the number they
// want to convert must be in
System.out.println("Please enter a base 10 number to convert:\n");
base10Num = scan.nextInt();
place0 = base10Num % base;
placeholder = base10Num / base;
place1 = placeholder % base;
placeholder /= base;
place2 = placeholder % base;
placeholder /= base;
place3 = placeholder % base;
placeholder /= base;
System.out.println(place3 + " " + place2 + " " + place1 + " " + place0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment