-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecToBin.java
More file actions
86 lines (80 loc) · 1.72 KB
/
Copy pathDecToBin.java
File metadata and controls
86 lines (80 loc) · 1.72 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.*;
public class DecToBin {
static String reverse(String s)
{
String s1="";
for(int i=s.length()-1;i>=0;i--)
s1+=s.charAt(i);
return s1;
}
int decToBin(int a) //Recursive Function to convert +ve no. into binary
{
if(a<=0)
return 0;
else
return (a%2)+10*decToBin(a/2);
}
String decBin(int a)//Function to return binary of +ve no. using String
{
String s="",s1="";
while(a>0)
{
int d=a%2;
s+=Integer.toString(d);
a/=2;
}
s1=reverse(s);
s="";
for(int i=0;i<(32-s1.length());i++)
s+="0";
s+=s1;
return s;
}
String negBin(String s)
{
String s1="";
int c=0;
for(int i=31;i>=0;i--)
{
if(c<1)
{
if(s.charAt(i)=='1')
{
s1+=s.charAt(i);
c++;
}
else
s1+=s.charAt(i);
}
else
{
if(s.charAt(i)=='1')
s1+="0";
else
s1+="1";
}
}
s=reverse(s1);
return s;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
DecToBin ob=new DecToBin();
System.out.println("Enter a number");
int a=sc.nextInt();
int b=Math.abs(a);
if(a>=0)
{
System.out.println("Binary using recursion \n"+ob.decToBin(a));
System.out.println("Binary using string \n"+ob.decBin(a));
System.out.println("Binary using inbuilt function \n"+Integer.toBinaryString(a));//Using inbuilt function to calculate binary of any number
}
else
{
String s=ob.decBin(b);
System.out.println("Binary using string \n"+ob.negBin(s));
System.out.println("Binary using inbuilt function \n"+Integer.toBinaryString(a));//Using inbuilt function to calculate binary of any number
}
}
}