We are given a decimal number and we will create a Kotlin program to convert decimal number to binary.
To convert decimal number to binary, we will recursively divide the decimal number by 2 and save the remainder found till the decimal number vanquishes.
Example:
Input:
Decimal number = 12
Recursive division:
Number |
Quotient |
Remainder |
12 |
6 |
0 |
6 |
3 |
0 |
3 |
1 |
1 |
1 |
0 |
1 |
Output:
Here, we will read the remainders in reverse order
which is the binary equivalent i.e.
1100 is the binary conversion for 12.
Program to convert decimal to binary in Kotlin
Output
need an explanation for this answer? contact us directly to get an explanation for this answer