atanasovetr

Numerical Systems

Jun 24th, 2021
648
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package MOPHomeworks;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class ConvertingToRandomSystem {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         System.out.print("Enter number: ");
  12.         int number = Integer.parseInt(reader.readLine());
  13.         System.out.print("Enter number system: ");
  14.         int numberSystem = Integer.parseInt(reader.readLine());
  15.         System.out.println(String.format("The number %d int %d number system is: %s",number,numberSystem,convertNumber(number,numberSystem)));
  16.     }
  17.  
  18.     static String convertNumber(int number, int numberSystem){
  19.         String result = "";
  20.  
  21.         while (number >= numberSystem){
  22.             result = result + (number % numberSystem + "");
  23.             number /= numberSystem;
  24.         }
  25.  
  26.         result +="" + number;
  27.  
  28.         return new StringBuilder(result).reverse().toString();
  29.     }
  30. }
RAW Paste Data