// ********************************************************
// Name:         Mohamed Aboutabl
// Date:         9/23/2004
// Assignment:   Lab 10 - BaseConvert.java
//
// *************************************************************
// References & Acknowledgements: I received no outside help
//
// ********************************************************
//   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 cs1.Keyboard;

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 of successive divisions

      String baseBNum; // the number in the new base
		baseBNum  =  ""; // initialize to the empty String

      // 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): ");
      base = Keyboard.readInt();

      // 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 = (int) Math.pow(base , 4) - 1 ;
		System.out.println("In base " + base 
			+ " you may now enter a number between 0 and " + maxNumber) ;

      System.out.print ("Please enter a base 10 number to convert: ");
      base10Num = Keyboard.readInt();
		
		System.out.print( "The decimal number, " + base10Num ) ;
    

      // 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 ;
		
		baseBNum = "" + place3 + place2 + place1 + place0 ;

      // Print the result (see notes in lab)
		System.out.println(", is equal to the base " + base 
			+ " number, " + baseBNum + "." ) ;

    }
}
