Write an algorithm that converts a decimal number to binary. Then, give the big-O for your algorithm.

c++ program that conver binary in to decimal examples

To convert a decimal number to binary, you can use the following algorithm:

1. Initialize an empty string to store the binary representation.
2. Use a loop to perform the following steps until the decimal number becomes zero:
a. Take the decimal number modulo 2 to get the remainder.
b. Append the remainder to the front of the binary representation string.
c. Divide the decimal number by 2.
3. Return the binary representation.

Here is the algorithm written in Python:

```python
def decimal_to_binary(decimal):
binary = ""

while decimal > 0:
remainder = decimal % 2
binary = str(remainder) + binary
decimal = decimal // 2

return binary
```

The time complexity of this algorithm is O(log n), where n is the decimal number. This is because in each iteration of the loop, we divide the decimal number by 2, effectively reducing its value by half. Thus, the number of iterations required is logarithmic in the value of the decimal number.

The space complexity of this algorithm is O(log n) since we need to store the binary representation of the number, which requires space proportional to the number of binary digits.