File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ public class binaryNumber_08 {
4
+
5
+ // Decimal To Binary
6
+
7
+ public static int Binary (int decimalNumber ){
8
+ int answer = 0 ;
9
+ int power = 1 ;
10
+ while (decimalNumber >0 ) {
11
+ int remainder = decimalNumber %2 ;
12
+ decimalNumber /=2 ;
13
+
14
+ answer += (remainder *power );
15
+ power *=10 ;
16
+ }
17
+ return answer ;
18
+ }
19
+
20
+ // Binary To Decimal
21
+
22
+ public static int Decimal (int BinaryNumber ){
23
+
24
+ int answer = 0 ;
25
+ int power = 1 ;
26
+ while (BinaryNumber >0 ) {
27
+ int remainder = BinaryNumber %10 ;
28
+ answer +=(remainder *power );
29
+
30
+ BinaryNumber /=10 ;
31
+ power *=2 ;
32
+
33
+ }
34
+ return answer ;
35
+ }
36
+
37
+
38
+ public static void main (String [] args ) {
39
+
40
+ Scanner sc = new Scanner (System .in );
41
+ System .out .println ("Enter the value :" );
42
+ int userInput = sc .nextInt ();
43
+
44
+ System .out .println (Binary (userInput ));
45
+
46
+ System .out .println (Decimal (userInput ));
47
+
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments