// Author : Khadiza Sultana #include<iostream> using namespace std; int decimalToBinary(int decNum){ int ans = 0, pow = 1; while(decNum > 0){ int rem = decNum % 2; // documenting the remainder for example for 3 % 2 = 1 decNum /= 2; // determining the divisor 3 / 2 = 1 ans += (rem * pow); // determining the position for the remainder as it goes from down to up pow *= 10; // updating the position of the digits } return ans; } int main(){ int decNum = 50; cout << decimalToBinary(decNum) << endl; return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter