forked from BimalRajGyawali/hacktoberfest-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DtoB.java
47 lines (46 loc) · 1.19 KB
/
DtoB.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
import java.util.*;
class DtoB
{
public static void binary(int n)
{
int f=0,c=0;
int a[]=new int[32];
for(int i=n;i!=0;i/=2)
{
a[c++]=i%2;
}
for(int i=c-1;i>=0;i--)
{
System.out.print(a[i]);}
}
public static void decimal(int n)
{
int c=0;int sum=0;
for(int i=n;i!=0;i/=10)
{
int rem=i%10;
sum+=rem*Math.pow(2,c++);
}
System.out.println(sum);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("1:Decimal to Binary");
System.out.println("2:Binary to Decimal");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter the decimal number you want to convert");
int n=sc.nextInt();
binary(n);
break;
case 2: System.out.println("Enter the binary number you want to convert");
int m=sc.nextInt();
decimal(m);
break;
default: System.out.println("OOPS! Wrong Input");
}
}
}