-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path86thProgram_DecimalToBinary.cpp
71 lines (67 loc) · 1.55 KB
/
86thProgram_DecimalToBinary.cpp
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
/*********
* Decimal to Binary:
* Given a decimal number, convert it to binary number.
* ************************************************************************/
#include<iostream>
using namespace std;
int main(){
int num ;
cout << "Enter the number : ";
cin >> num;
int temp = num;
int temp2 = num;
int c = 0;
int base =1;
int binary = 0;
int rem;
while(temp!=0){
temp = temp/2;
c = c+1;
}
for(int i=1; i<=c; i++){
rem = temp2%2;
binary = binary + rem*base;
temp2 = temp2/2;
base = base*10;
}
cout << "Binary Value of :" << num<< "is :" << binary << "\n";
return 0;
}
/****************************************************************
* Working of the above snippet:
* ***************************************************************
* num = 2 , temp = temp2 = num =2;
* c = 0;
* base =1;
* binary = 0;
* while(temp!=0){
* temp = 2/2 = 1;
* c = 0+1 =1;
*
* Again,
* temp = 1/2 = 0;
* c = 1+1 =2;
*
* }
*
* for int i = 1 to 2 ;
* i = 1;
* rem = 2%2 = 0;
* binary = 0 + 0 x 1 =0;
* temp2 = 2/2 = 1;
* base = 1*10 = 10;
*
* i =2;
* rem = 1%2 = 1;
* binary = 0 + 1 x 10 =10;
* temp2 = 1/2 = 0;
* base = 10*10 = 100;
*
* Hence binary = 10
* Note : Base of Binary is 2 and Decimal is 10.
*
*
*
*
*
* ****************************************************************/