-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberConversion.java
More file actions
54 lines (43 loc) · 1.45 KB
/
NumberConversion.java
File metadata and controls
54 lines (43 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Convert user input decimal number to binary,octal or hexadecimal.
import java.util.Scanner;
public class NumberConversion {
int decimal;
Scanner userInput = new Scanner(System.in);
void toBinary() {
System.out.println("Please enter your Decimal Number: ");
decimal = userInput.nextInt();
String binary = Integer.toBinaryString(decimal);
System.out.println(decimal+ " binary number is = "+binary);
}
void toOctal() {
System.out.println("Please enter your Decimal Number: ");
decimal = userInput.nextInt();
String octal = Integer.toOctalString(decimal);
System.out.println(decimal+ " Octal number is = "+octal);
}
void toHexa() {
System.out.println("Please enter your Decimal Number: ");
decimal = userInput.nextInt();
String Hexadecimal = Integer.toHexString(decimal);
System.out.println(decimal+ " Hexadecimal number is = "+Hexadecimal);
}
public static void main(String[] args) {
NumberConversion obj = new NumberConversion();
Scanner userInput = new Scanner(System.in);
System.out.println("1. Decimal to Binary\n2. Decimal to Octal\n3. Decimal to Hexadecimal\n\n");
System.out.println("Please enter a value to convert: ");
int choice = userInput.nextInt();
if(choice == 1 ) {
obj.toBinary();
}
else if (choice == 2) {
obj.toOctal();
}
else if( choice == 3) {
obj.toHexa();
}
else {
System.out.println("Wrong Input, Please run the programe again and enter a value between 1-3");
}
}
}