-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber12.java
More file actions
50 lines (46 loc) · 768 Bytes
/
Number12.java
File metadata and controls
50 lines (46 loc) · 768 Bytes
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
import java.util.*;
public class Number12 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,i;
String p,q="",c="1";
System.out.println("Enter a number");
n=sc.nextInt();
p=Integer.toBinaryString(n);
for(i=0;i<p.length();i++)
{
if(p.charAt(i)=='1')
q=q+"0";
else
q=q+"1";
}
q="1"+q;
p="";
for(i=q.length()-1;i>=0;i--)
{
if(q.charAt(i)=='1'&&c=="1")
{
p="0"+p;
c="1";
}
else if(q.charAt(i)=='1'&&c=="0")
{
p="1"+p;
c="0";
}
else if(q.charAt(i)=='0'&&c=="1")
{
p="0"+p;
c="0";
}
else
{
p="0"+p;
c="0";
}
}
if(c=="1")
p="1"+p;
System.out.println(p);
}
}