Advertisement
Guest User

jea edited

a guest
Aug 3rd, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.63 KB | None | 0 0
  1. import java.util.*;
  2. public class FamilyNameNumberSystem{
  3.  
  4. public static void main(String args[]){
  5. Scanner scan = new Scanner(System.in);
  6. int option;
  7. System.out.println("-----SELECT OPERATION------ ");
  8. System.out.println("1. Decimal to Binary.");
  9. System.out.println("2. Decimal to Octal");
  10. System.out.println("3. Decimal to Hexa");
  11. System.out.println("4. Binary to Decimal");
  12. System.out.println("5. Binary to Octal");
  13. System.out.println("6. Binary to Hexa");
  14. System.out.println("7. Octal to Decimal");
  15. System.out.println("8. Octal to Binary");
  16. System.out.println("9. Octal to Hexa");
  17. System.out.println("10. Hexa to Decimal ");
  18. System.out.println("11. Hexa to Binary");
  19. System.out.println("12. Hexa to Octal");
  20. System.out.println("-----------------");
  21. System.out.print("Enter Option: ");
  22. option = scan.nextInt();
  23. System.out.println();
  24. switch(option){
  25. case 1:Decimal2BinaryR();break;
  26. case 2:Decimal2OctalR();break;
  27. case 3:Decimal2HexaR();break;
  28. case 4:Binary2DecimalR();break;
  29. case 5: BinaryToOctalR();break;
  30. case 6: BinaryToHexaR();break;
  31. case 7:Octal2DecimalR();break;
  32. case 8: OctalToBinaryR();break;
  33. case 9: Octal2HexaR();break;
  34. case 10: HexToDecimalR();break;
  35. case 11: HexaToBinaryR();break;
  36. case 12:HexaToOctal();break;
  37. }
  38. System.out.println();
  39. }
  40. public static void Decimal2BinaryR(){
  41.  
  42. Scanner scan = new Scanner(System.in);
  43. int decimal;
  44. int base = 2;
  45. System.out.println();
  46. System.out.print("Enter Decimal: ");
  47. decimal = scan.nextInt();
  48. System.out.print("Decimal "+decimal+" to Binary is: ");
  49. decimalToBinary(decimal,base);
  50.  
  51. }
  52. public static void decimalToBinary(int num,int base){
  53.  
  54. if(num==0){
  55. System.out.print("");
  56. }else if(num > 0){
  57. decimalToBinary(num/base,base);
  58. System.out.print(num%base);
  59. }
  60.  
  61. }
  62.  
  63.  
  64. public static void Decimal2OctalR(){
  65.  
  66. Scanner scan = new Scanner(System.in);
  67. int decimal;
  68. int base = 8;
  69. System.out.println();
  70. System.out.print("Enter Decimal: ");
  71. decimal = scan.nextInt();
  72. System.out.print("Decimal "+decimal+ " to Octal is: ");
  73. decimalToOctal(decimal,base);
  74.  
  75. }
  76. public static void decimalToOctal(int num,int base){
  77.  
  78. if(num==0){
  79. return;
  80. }else if(num > 0){
  81. decimalToOctal(num/base,base);
  82. System.out.print(num%base);
  83. }
  84. }
  85.  
  86. public static void Decimal2HexaR(){
  87.  
  88. Scanner scan = new Scanner(System.in);
  89. int decimal;
  90. int base = 16;
  91. System.out.println();
  92. System.out.print("Enter Decimal: ");
  93. decimal = scan.nextInt();
  94. System.out.print("Decimal "+decimal+" to Hexadecimal is: ");
  95. decimalToHexa(decimal,base);
  96.  
  97. }
  98. public static void decimalToHexa(int num,int base){
  99.  
  100. if(num==0){
  101. return ;
  102. }else if(num > 0){
  103. decimalToHexa(num/base,base);
  104. display(num%base);
  105. }
  106. }
  107. public static void display(int num){
  108.  
  109. if(num<=9){
  110. System.out.print(num);
  111. }else{
  112. for(int i=10;i<=16;i++){
  113. if(num==i){
  114. System.out.printf("%c",i+55);
  115. }
  116. }
  117. }
  118. }
  119.  
  120. public static void Binary2DecimalR(){
  121.  
  122. Scanner scan = new Scanner(System.in);
  123. int base =0;
  124. int binary;
  125. System.out.println();
  126. System.out.print("Enter Binary: ");
  127. binary = scan.nextInt();
  128. System.out.print("Binary "+binary+" to Decimal is: "+binaryToDecimal(binary,base));
  129.  
  130.  
  131. }
  132.  
  133. public static int binaryToDecimal(int number, int base){
  134. if(number==0){
  135. return 0;
  136. }else{
  137. return(number%2)*(int)Math.pow(2,base) +
  138. binaryToDecimal(number/10,base+1);
  139. }
  140. }
  141. public static void HexaToOctal(){
  142. Scanner scan = new Scanner(System.in);
  143. String num;
  144. System.out.println();
  145. System.out.print("Enter Hexa: ");
  146. num = scan.nextLine();
  147. Integer outputDecimal = Integer.parseInt(num, 16);
  148. System.out.print("Hexadecimal "+num+" to Octal is ");
  149. decimalToOctal(outputDecimal,8);
  150. }
  151. public static void BinaryToHexaR(){
  152. Scanner scan = new Scanner(System.in);
  153. int num ;
  154. int hexa;
  155. System.out.println();
  156. System.out.print("Enter Binary: ");
  157. num = scan.nextInt();
  158.  
  159. hexa = binaryToDecimal(num,0);
  160.  
  161. System.out.println("Binary " + num + " to Hexadecimal is: ");
  162. decimalToHexa(hexa,16);
  163. System.out.println();
  164.  
  165. }
  166.  
  167. public static void Octal2DecimalR(){
  168.  
  169. Scanner scan = new Scanner(System.in);
  170.  
  171. int base = 8;
  172. int octal;
  173. System.out.println();
  174. System.out.print("Enter Octal: ");
  175. octal = scan.nextInt();
  176. System.out.print("Octal "+octal+" to Decimal is "+octalToDecimal(octal,base,0));
  177.  
  178.  
  179. }
  180. public static int octalToDecimal(int num,int base,int i){
  181.  
  182. if(num == 0){
  183. return 0;
  184. }else{
  185. return (num%10) * (int)Math.pow(base,i)+
  186. octalToDecimal(num/10,base,i+1);
  187. }
  188.  
  189. }
  190.  
  191. public static void HexToDecimalR(){
  192.  
  193. System.out.println();
  194. System.out.print("Enter Hexadecimal:");
  195. Scanner s = new Scanner(System.in);
  196. String inputHex = s.nextLine();
  197.  
  198. Integer outputDecimal = Integer.parseInt(inputHex, 16);
  199.  
  200. System.out.println("Hexadecimal "+inputHex+" to Decimal is: "+outputDecimal);
  201.  
  202. }
  203.  
  204. public static void OctalToBinaryR(){
  205.  
  206. Scanner scan = new Scanner(System.in);
  207.  
  208. int octal;
  209. int reverse =0;
  210. int temp;
  211. System.out.println();
  212. System.out.print("Enter Octal: ");
  213. octal = scan.nextInt();
  214. temp = octal;
  215. while(octal!=0){
  216. reverse = reverse * 10;
  217. reverse = reverse + octal%10;
  218. octal = octal/10;
  219. }
  220. System.out.print("Octal "+temp+" to Binary is: ");
  221. octalToBinary(reverse);
  222.  
  223. }
  224. public static void octalToBinary(int octal){
  225.  
  226. int temp = octal;
  227. octal = octal%10;
  228. if(octal==0){
  229. System.out.print("");
  230. }else{
  231. if(octal==1){
  232. System.out.print("001");
  233. }else if(octal==2){
  234. System.out.print("010");
  235. }else if(octal==3){
  236. System.out.print("011");
  237. }else if(octal==4){
  238. System.out.print("100");
  239. }else if(octal==5){
  240. System.out.print("101");
  241. }else if(octal==6){
  242. System.out.print("110");
  243. }else if(octal==7){
  244. System.out.print("111");
  245. }
  246. octalToBinary(temp/10);
  247. }
  248. }
  249. public static void Octal2HexaR(){
  250.  
  251. Scanner scan = new Scanner(System.in);
  252. String num;
  253. System.out.println();
  254. System.out.print("Enter Octal: ");
  255. num = scan.nextLine();
  256. Integer outputDecimal = Integer.parseInt(num, 8);
  257. System.out.print("Octal "+num+" to Hexadecimal is: ");
  258. decimalToHexa(outputDecimal,16);
  259.  
  260.  
  261. }
  262.  
  263. public static void HexaToBinaryR(){
  264.  
  265. Scanner scan = new Scanner(System.in);
  266.  
  267. String hexa;
  268. String reverse ="";
  269. System.out.println();
  270. System.out.print("Enter Hexa: ");
  271. hexa = scan.nextLine();
  272. System.out.print("Hexadecimal " + hexa+" to Binary is: ");
  273. hexaToBinary(hexa,0);
  274.  
  275. }
  276. public static void BinaryToOctalR(){
  277. Scanner scan = new Scanner(System.in);
  278. int num ;
  279. int octal;
  280. System.out.println();
  281. System.out.print("Enter Binary: ");
  282. num = scan.nextInt();
  283. octal = binaryToDecimal(num,0);
  284.  
  285.  
  286. System.out.print("Binary " + num+" to Octal is: ");
  287. decimalToOctal(octal,8);
  288. System.out.println();
  289. }
  290. public static void hexaToBinary(String hexa,int i){
  291.  
  292. if(i==hexa.length()){
  293. System.out.print("");
  294. }else{
  295. if(hexa.charAt(i)=='0'){ System.out.print("0000");}
  296. else if(hexa.charAt(i)=='1') {System.out.print("0001");}
  297. else if(hexa.charAt(i)=='2') {System.out.print("0010");}
  298. else if(hexa.charAt(i)=='3') {System.out.print("0011");}
  299. else if(hexa.charAt(i)=='4') {System.out.print("0100");}
  300. else if(hexa.charAt(i)=='5') {System.out.print("0101");}
  301. else if(hexa.charAt(i)=='6') {System.out.print("0110");}
  302. else if(hexa.charAt(i)=='7') {System.out.print("0111");}
  303. else if(hexa.charAt(i)=='8') {System.out.print("1000");}
  304. else if(hexa.charAt(i)=='9') {System.out.print("1001");}
  305. else if(hexa.charAt(i)=='a'||hexa.charAt(i)=='A')
  306. {System.out.print("1010");}
  307. else if(hexa.charAt(i)=='b'||hexa.charAt(i)=='B')
  308. {System.out.print("1011");}
  309. else if(hexa.charAt(i)=='c'||hexa.charAt(i)=='C')
  310. {System.out.print("1100");}
  311. else if(hexa.charAt(i)=='d'||hexa.charAt(i)=='D')
  312. {System.out.print("1101");}
  313. else if(hexa.charAt(i)=='e'||hexa.charAt(i)=='E')
  314. {System.out.print("1110");}
  315. else if(hexa.charAt(i)=='f'||hexa.charAt(i)=='F')
  316. {System.out.print("1111");}
  317.  
  318.  
  319. hexaToBinary(hexa,i+1);
  320.  
  321. }
  322. }
  323.  
  324.  
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement