Skip to content

Commit b043f06

Browse files
authored
Add files via upload
Binary To Decimal and vice versa By using Method
1 parent d29a82d commit b043f06

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

binaryNumber_08.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

0 commit comments

Comments
 (0)