forked from zahinekbal/codeWith-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImakshat47_decimal2binaryBasicArray.cpp
More file actions
63 lines (46 loc) · 1016 Bytes
/
Imakshat47_decimal2binaryBasicArray.cpp
File metadata and controls
63 lines (46 loc) · 1016 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
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
# Program for Decimal to Binary Conversion
Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent binary number.
Examples:
Input : 7
Output : 111
Input : 10
Output : 1010
Input: 33
Output: 100001
*/
#include <iostream>
using namespace std;
int *dec2bin(int num)
{
int *binaryNum = new int[33];
int i = 0;
while (num > 0)
{
binaryNum[i] = num % 2;
num = num / 2;
++i;
}
binaryNum[32] = i - 1;
return binaryNum;
}
int main()
{
int t;
scanf("%d\n", &t);
while (t--)
{
int num;
scanf("%d", &num);
if (num == 0)
{
printf("0\n");
continue;
}
int *res = dec2bin(num);
for (int j = res[32]; j >= 0; --j)
printf("%d", res[j]);
printf("\n");
}
return 0;
}