Skip to content

Commit 7b14abe

Browse files
authored
Create Program to Convert a decimal number to corressponding binary number
1 parent d819d8d commit 7b14abe

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// function to convert decimal number to corressponding binary number
5+
void decToBinary(int n)
6+
{
7+
// array to store binary number
8+
int binaryNum[32];
9+
10+
// counter for binary array
11+
int i = 0;
12+
while (n > 0) {
13+
14+
// storing remainder in binary array
15+
binaryNum[i] = n % 2;
16+
n = n / 2;
17+
i++;
18+
}
19+
20+
// printing binary array in reverse order
21+
for (int j = i - 1; j >= 0; j--)
22+
cout << binaryNum[j];
23+
}
24+
25+
// Driver program to test above function
26+
int main()
27+
{
28+
int n = 17;
29+
decToBinary(n);
30+
return 0;
31+
}

0 commit comments

Comments
 (0)