-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryNumber_08.java
49 lines (34 loc) Β· 1.09 KB
/
binaryNumber_08.java
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
import java.util.Scanner;
public class binaryNumber_08 {
// Decimal To Binary
public static int Binary(int decimalNumber){
int answer = 0 ;
int power = 1;
while (decimalNumber>0) {
int remainder = decimalNumber%2 ;
decimalNumber /=2 ;
answer += (remainder*power);
power*=10 ;
}
return answer ;
}
// Binary To Decimal
public static int Decimal(int BinaryNumber){
int answer = 0 ;
int power = 1 ;
while (BinaryNumber>0) {
int remainder = BinaryNumber%10 ;
answer+=(remainder*power);
BinaryNumber/=10 ;
power*=2;
}
return answer ;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value :");
int userInput = sc.nextInt();
System.out.println(Binary(userInput));
System.out.println(Decimal(userInput));
}
}