Created
November 30, 2019 20:45
-
-
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++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************* | |
* 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