10473 Simple Base Conversion(solve in Java)
Posted by Unknown on Thursday, August 13, 2015 with No comments
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
while (s != null) {
int num;
boolean hexa = false;
if (s.charAt(0) == '0' && s.charAt(1) == 'x') {
hexa = true;
} else {
num = Integer.parseInt(s);
if (num < 0)
break;
}
if (hexa == false) {
int dec = Integer.parseInt(s);
String hex = Integer.toHexString(dec);
System.out.println("0x" + hex.toUpperCase());
} else {
String inputHex = s.substring(2, s.length());
Integer outputDecimal = Integer.parseInt(inputHex, 16);
System.out.println(outputDecimal);
}
s = br.readLine();
}
}
}
Coded by sudipto
0 comments:
Post a Comment