The following code is to convert the number i.e., Decimal Number to either Binary or Octal or Hexadecimal Number according to user's choice.
Code:-
import java.util.*;
public class Convert {
Scanner scan = new Scanner(System.in);
int choice, dev;
String r = "\0";
long num, res, rem, temp;
public void input() {
try {
System.out.printf("1: Decimal to Binary\n2: Decimal to Octal\n3: Decal to Hexadecimal\nEnter your choice: ");
choice = scan.nextInt();
System.out.printf("Enter number: ");
num = scan.nextLong();
} catch (NumberFormatException | InputMismatchException e) {
System.err.println("Error Occur!\n" + e.getMessage());
System.exit(0);
}
}
public void compute() {
switch (choice) {
case 1:
dev = 2;
break;
case 2:
dev = 8;
break;
case 3:
dev = 16;
break;
default:
System.err.println("Invalid Choice!");
System.exit(0);
}
temp = num;
char cha;
while (temp != 0) {
rem = temp % dev;
temp /= dev;
if (rem < 10) {
r = r.concat(Long.toString(rem));
}
if (rem > 9) {
cha = hexadecimal(rem);
r = r.concat(Character.toString(cha));
}
}
StringBuffer str = (new StringBuffer(r)).reverse();
r = str.toString();
System.out.println("Converted Number: " + r);
}
public char hexadecimal(long re) {
char cha;
if (re == 10) {
cha = 'A';
} else if (re == 11) {
cha = 'B';
} else if (re == 12) {
cha = 'C';
} else if (re == 13) {
cha = 'D';
} else if (re == 14) {
cha = 'E';
} else {
cha = 'F';
}
return cha;
}
public static void main(String[] args) {
Convert con = new Convert();
con.input();
con.compute();
}
}
Output:-
1: Decimal to Binary
2: Decimal to Octal
3: Decal to Hexadecimal
Enter your choice: 3
Enter number: 50
Converted Number: 32
Code:-
import java.util.*;
public class Convert {
Scanner scan = new Scanner(System.in);
int choice, dev;
String r = "\0";
long num, res, rem, temp;
public void input() {
try {
System.out.printf("1: Decimal to Binary\n2: Decimal to Octal\n3: Decal to Hexadecimal\nEnter your choice: ");
choice = scan.nextInt();
System.out.printf("Enter number: ");
num = scan.nextLong();
} catch (NumberFormatException | InputMismatchException e) {
System.err.println("Error Occur!\n" + e.getMessage());
System.exit(0);
}
}
public void compute() {
switch (choice) {
case 1:
dev = 2;
break;
case 2:
dev = 8;
break;
case 3:
dev = 16;
break;
default:
System.err.println("Invalid Choice!");
System.exit(0);
}
temp = num;
char cha;
while (temp != 0) {
rem = temp % dev;
temp /= dev;
if (rem < 10) {
r = r.concat(Long.toString(rem));
}
if (rem > 9) {
cha = hexadecimal(rem);
r = r.concat(Character.toString(cha));
}
}
StringBuffer str = (new StringBuffer(r)).reverse();
r = str.toString();
System.out.println("Converted Number: " + r);
}
public char hexadecimal(long re) {
char cha;
if (re == 10) {
cha = 'A';
} else if (re == 11) {
cha = 'B';
} else if (re == 12) {
cha = 'C';
} else if (re == 13) {
cha = 'D';
} else if (re == 14) {
cha = 'E';
} else {
cha = 'F';
}
return cha;
}
public static void main(String[] args) {
Convert con = new Convert();
con.input();
con.compute();
}
}
Output:-
1: Decimal to Binary
2: Decimal to Octal
3: Decal to Hexadecimal
Enter your choice: 3
Enter number: 50
Converted Number: 32
Comments