forked from zahinekbal/codeWith-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecimal2binaryUsingBitwise.c
More file actions
53 lines (43 loc) · 817 Bytes
/
decimal2binaryUsingBitwise.c
File metadata and controls
53 lines (43 loc) · 817 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
/*
# 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 <stdio.h>
void dec2bin(int num)
{
for (int i = 31; i >= 0; --i)
{
int k = num >> i;
if (k & 1)
printf("1");
else
{
printf("0");
}
}
printf("\n");
}
int main()
{
int t;
scanf("%d\n", &t);
while (t--)
{
int num;
scanf("%d", &num);
if (num == 0)
{
printf("0\n");
continue;
}
dec2bin(num);
}
return 0;
}