forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert_decimal_to_binary.cpp
52 lines (41 loc) · 1 KB
/
Convert_decimal_to_binary.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
//C++ Program to convert a positive decimal number into its equivalent binary number
#include <bits/stdc++.h>
using namespace std;
//Function to convert a positive decimal number into a binary number
long long convertDecimalToBinary(int n) {
long long binaryNumber = 0;
int remainder, i = 1;
while (n != 0) {
remainder = n % 2;
n /= 2;
binaryNumber += remainder * i;
i *= 10;
}
return binaryNumber;
}
//Driver function
int main() {
int decimal;
//Prompts user for input
cout << "Enter a decimal number to be converted into binary: ";
cin >> decimal;
//Display the resultant binary number
cout << "Binary number: " << convertDecimalToBinary(decimal) << "\n";
return 0;
}
/*
Input:
Enter a decimal number to be converted into binary: 13
Output:
Binary number: 1101
Input:
Enter a decimal number to be converted into binary: 57
Output:
Binary number: 111001
Input:
Enter a decimal number to be converted into binary: 100
Output:
Binary number: 1100100
Time complexity: O(N)
Space complexity: O(1)
*/