-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29-dec-bin.cpp
35 lines (22 loc) · 904 Bytes
/
29-dec-bin.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
#include <iostream>
using namespace std;
// Manually converet decimal to binary.
int DecToBin(int N) {
int multiplyer = 1; // fot arranging digits. like : 1*(once digit) + 10*(tens digit) + ...
int finalBin = 0; // final output
// loop for continuous division by 2 , to find remainders and making finalBin number using the digits:
while (N >=1){
int digit = N % 2; // getting the binARY DIGIT.
N /= 2; // N = N/2 for next iteration
finalBin += digit * multiplyer; // NUMBER = ONCE*1 TENS*10 THOUSANDS*1000 + ...
multiplyer *= 10; // 1 10 10 1000 ...
}
return finalBin;
}
int main(){
int dec;
cout << "Enter the decimal number: "; cin >> dec;
cout << "The binary value of the decimal number " << dec << " is: " << DecToBin(dec) ;
return 0;
}
// Mohammad Maasir | 9 September 2023 | 12:24 - 12:44