Skip to content

Commit f817ee2

Browse files
authored
Create Decimal_to_Binary.cpp
1 parent ddb1946 commit f817ee2

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Decimal_to_Binary.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
next →← prev
3+
C++ Program to convert Decimal to Binary
4+
We can convert any decimal number (base-10 (0 to 9)) into binary number (base-2 (0 or 1)) by C++ program.
5+
6+
Decimal Number
7+
Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 223, 585, 192, 0, 7 etc.
8+
9+
Binary Number
10+
Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is binary number such as 1001, 101, 11111, 101010 etc.
11+
12+
Let's see the some binary numbers for the decimal number.
13+
14+
Decimal Binary
15+
1 0
16+
2 10
17+
3 11
18+
4 100
19+
5 101
20+
6 110
21+
7 111
22+
8 1000
23+
9 1001
24+
10 1010
25+
26+
27+
Decimal to Binary Conversion Algorithm
28+
Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in array
29+
30+
Step 2: Divide the number by 2 through / (division operator)
31+
32+
Step 3: Repeat the step 2 until the number is greater than zero
33+
34+
Let's see the C++ example to convert decimal to binary.
35+
36+
#include <iostream>
37+
using namespace std;
38+
int main()
39+
{
40+
int a[10], n, i;
41+
cout<<"Enter the number to convert: ";
42+
cin>>n;
43+
for(i=0; n>0; i++)
44+
{
45+
a[i]=n%2;
46+
n= n/2;
47+
}
48+
cout<<"Binary of the given number= ";
49+
for(i=i-1 ;i>=0 ;i--)
50+
{
51+
cout<<a[i];
52+
}
53+
}

0 commit comments

Comments
 (0)