Skip to content

Instantly share code, notes, and snippets.

@javiermontenegro
Created November 30, 2019 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javiermontenegro/0bdd4243fffed52e3723b1c843452fb2 to your computer and use it in GitHub Desktop.
Save javiermontenegro/0bdd4243fffed52e3723b1c843452fb2 to your computer and use it in GitHub Desktop.
This gist is a example of how to convert decimal to binary in C++
/*********************************************************************
* Filename: Cpp_Decimal.To.Binary.cpp
* Author: Javier Montenegro (javiermontenegro.github.io)
* Copyright: @2019
* Details: this gist is a example of how to convert decimal to binary in C++
*********************************************************************/
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
int number;
cout << "Enter a number:";
cin >> number;
int remainder, binary = 0, var = 1;
do {
remainder = number % 2;
number = number / 2;
binary = binary + (remainder*var);
var = var * 10;
} while (number>0);
cout << "The binary is :";
cout << binary;
cout << endl;
return 0;
}//End main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment