forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DecimalToBinary.c
88 lines (69 loc) · 1.5 KB
/
DecimalToBinary.c
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
87
88
/*
AIM :: To take any decimal number from the user
and convert that given decimal number into binary number
using stack (by Array represention).
*/
#include <stdio.h>
//MACRO definition
#define SIZE 50
//Array representation of stack
int stack[SIZE];
int top = -1;
//function to check whether stack is empty or not by returning 1 for empty and 0 for non-empty
int isEmpty()
{
if (top == -1)
return 1;
return 0;
}
//function to check whether stack is full or not by returning 1 for full and 0 for not-full
int isFull()
{
if (top == SIZE - 1)
return 1;
return 0;
}
//function to insert value into the stack
void push(int r)
{
if (isFull())
printf("\nSTACK OVERFLOW\n");
else
stack[++top] = r;
}
//function for delete value from the stack
int pop()
{
if (isEmpty())
printf("\nSTACK UNDERFLOW\n");
else
return stack[top--];
}
int main()
{
//number for storing decimal number given by user
int number, num, reminder;
printf("\nEnter decimal number: ");
scanf("%d", &number);
//preserving original number for further use
num = number;
while (num >= 1)
{
reminder = num % 2;
push(reminder);
num = num / 2;
}
printf("\nDECIMAL :: %d\n", number);
printf("BINARY :: ");
while (!isEmpty())
printf("%d", pop());
return 0;
}
/*
TEST CASE
Enter decimal number: 10
DECIMAL :: 10
BINARY :: 1010
TIME COMPLEXITY :: O(n)
SPACE COMPLEXITY :: O(1)
*/