Solution Review: Convert Decimal Number to Binary
Let's go over the solution review of the challenge given in the previous lesson.
We'll cover the following...
Solution #
Press + to interact
#include <iostream>using namespace std;int main() {// Initialize variablesint decimal = 10, binary = 0;int remainder, product = 1;// Prints value of decimalcout << "Decimal Number = " << decimal << endl;// while block/*Checks if the value of `decimal` is not equal to `0`.If yes, then execute line No. 17 to 21.If no, then execute line No. 23.*/while (decimal != 0) {remainder = decimal % 2;binary = binary + (remainder * product);decimal = decimal / 2;product *= 10;}// while exitcout << "Binary Number = " << binary;return 0;}
Explanation
To convert the decimal number into ...