Convert Decimal to Binary
C++
Hard
2 views
Problem Description
Convert decimal number to binary (base 2) representation.
Real Life: How computers store all data internally.
Step-by-Step Logic:
1. Take decimal number
2. Divide by 2 repeatedly
3. Store remainder each time (will be 0 or 1)
4. Print remainders in reverse order
5. That's your binary number
Official Solution
void control_q14_decimal_to_binary() {
int decimal = 25;
int binary[32];
int index = 0;
int temp = decimal;
while(temp > 0) {
binary[index] = temp % 2;
temp = temp / 2;
index++;
}
cout << "Decimal " << decimal << " in binary: ";
for(int i = index - 1; i >= 0; i--) {
cout << binary[i];
}
cout << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!