File tree 2 files changed +53
-0
lines changed
2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ public class BinaryDecimalConverter {
4
+ public static void main (String [] args ) {
5
+ Scanner scanner = new Scanner (System .in );
6
+
7
+ System .out .println ("Binary to Decimal and Decimal to Binary Converter" );
8
+ System .out .print ("Enter 1 to convert Binary to Decimal or 2 to convert Decimal to Binary: " );
9
+ int choice = scanner .nextInt ();
10
+
11
+ if (choice == 1 ) {
12
+ System .out .print ("Enter a binary number: " );
13
+ String binaryInput = scanner .next ();
14
+ int decimalResult = binaryToDecimal (binaryInput );
15
+ System .out .println ("Decimal result: " + decimalResult );
16
+ } else if (choice == 2 ) {
17
+ System .out .print ("Enter a decimal number: " );
18
+ int decimalInput = scanner .nextInt ();
19
+ String binaryResult = decimalToBinary (decimalInput );
20
+ System .out .println ("Binary result: " + binaryResult );
21
+ } else {
22
+ System .out .println ("Invalid choice. Please enter 1 for Binary to Decimal or 2 for Decimal to Binary." );
23
+ }
24
+ }
25
+
26
+ public static int binaryToDecimal (String binary ) {
27
+ return Integer .parseInt (binary , 2 );
28
+ }
29
+
30
+ public static String decimalToBinary (int decimal ) {
31
+ return Integer .toBinaryString (decimal );
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ # Binary to Decimal and Decimal to Binary Converter
2
+
3
+ This is a simple Java program that allows you to convert between Binary and Decimal numbers.
4
+
5
+ ## How to Use
6
+
7
+ 1 . Compile and run ` BinaryDecimalConverter.java ` .
8
+ 2 . Choose option 1 to convert Binary to Decimal or option 2 to convert Decimal to Binary.
9
+ 3 . Follow the on-screen instructions to enter the number for conversion.
10
+
11
+ ## Features
12
+
13
+ - Converts Binary to Decimal and Decimal to Binary.
14
+ - Simple command-line interface for ease of use.
15
+
16
+ ## Technologies Used
17
+
18
+ - Java
19
+
20
+ Feel free to modify the code or integrate it into other Java applications as needed.
You can’t perform that action at this time.
0 commit comments