-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decimal_to_Binary.java
53 lines (44 loc) · 1.03 KB
/
Decimal_to_Binary.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
50
51
52
53
//WAP to convert Decimal No. To Binary. using concept of Stack
import java.util.*;
class Stacks4 {
int top;
int size;
int sa[];
Stacks4(int s) {
size = s;
top = -1;
sa = new int[size];
}
void push(int a) {
if (top >= size - 1) {
System.out.println("Stack Overflow.");
} else {
top++;
sa[top] = a;
}
}
void pop() {
if (top == -1) {
System.out.println("Stack is Underflow");
} else {
top--;
System.out.print(sa[top + 1]);
}
}
}
class Run2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Decimal No.");
int no = sc.nextInt();
Stacks4 s1 = new Stacks4(8000);
while (no > 0) {
s1.push(no % 2);
no = no / 2;
}
System.out.println("Your Binary Number is: ");
while (s1.top > -1) {
s1.pop();
}
}
}