This post Contains a total of 43+ CPP Decimal to Binary Converter Program Examples with Source Code. All these Decimal to Binary converters are made using C++ Programming Language.
You can use the source code of these examples with credits to the original owner.
Related Posts
CPP Decimal to Binary Converter Programs
1. By Harsh Kumar
Made by Harsh Kumar. Enter decimal number to get binary number. Source
Decimal number: 10 After conversion Binary number: 1010
#include <iostream>
using namespace std;
int main() {
long int dec,red[100],quo,i=1,j;
cin>>dec;
quo=dec;
if(quo<0)
{
cout<<"Not Possible Conversion";
}
else if(quo==0)
{
cout<<"Decimal number:\n"<<dec;
cout <<"\nAfter conversion";
cout <<"\nBinary number:\n"<<quo;
}
else
{
while (quo!=0)
{
red[i]=quo%2;
quo=quo/2;
i++;
}
cout<<"Decimal number:\n"<<dec;
cout <<"\nAfter conversion";
cout <<"\nBinary number:\n";
for (j=i-1;j>0;j--)
{
cout<<red[j];
}
return 0;}
}
2. By Yoshideveloper
Made by Yoshideveloper. Simple C++ Program to Convert Decimal number to binary. Source
The binary form of 11 is 1011
#include<iostream>
using namespace std;
void binary(int num)
{
int rem;
if (num <= 1)
{
cout << num;
return;
}
rem = num % 2;
binary(num / 2);
cout << rem;
}
int main()
{
int dec, bin;
cin >> dec;
if (dec < 0)
cout << dec << " is not a positive number." << endl;
else
{
cout << "The binary form of " << dec << " is ";
binary(dec);
cout << endl;
}
return 0;
}
3. By Babak
Made by Babak. A simple base converter from Dec to Bin and capable to handle NEGATIVE decimals to produce 2’s complement. Source
12 = 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1100
/* Code name:
* By : Babak Sheykhan (Pers)
* Date : Aug 1, 2017
* /////////////////////////////////////////////////
* Change log #1 (v.0.02) - Aug 2, 2017
*
* 1. Converting to Hexadecimal added
* 2. Solve some minor bugs
*
* /////////////////////////////////////////////////
* Change log #2 (v.0.03) - Aug 29, 2017
* [** Special thanks to my mentor JPM7 for his guidance and positive feedbacks **]
*
* 1. Optimizing the code using bitwise and shift operator.
* 2. Easier to read binary in output
*
*/
#include <iostream>
#include <vector>
#include <string>
#include <climits> // CHAR_BIT
using std::cout; using std::cin; using std::endl;
// Prototypes
bool isDec(long long d);
void converter(long long d, std::vector<int> &vHex);
int main() {
cout << "NOTE: Numbers greater than 9,223,372,036,854,775,807 and\n"
" less than -9,223,372,036,854,775,807 are Invalid.\n\n";
/*---- Hex Memory ----*/
std::vector<int> memHex(16, 0);
/*---- Holds Decimal ----*/
long long dec = 0;
/**** Input eval loop ****/
do {
cout << "Enter a positive or negative decimal number: ";
cin >> dec;
} while (!isDec(dec));
cout << "\nConverting to binary and hexadecimal...\n\n";
cout << dec << " = \n";
converter(dec, memHex);
}
bool isDec(long long d) {
/**** Error Handling ****/
// Handling Non-numeric and too long input
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(200, '\n');
cout << "Invalid input!\n";
return false;
}
else return true;
}
void converter(long long d, std::vector<int> &vHex)
{
//if (d < 0) ~d; // ! DUMB MISTAKE !
std::string lookupHex = "0123456789ABCDEF";
short lookupPow[] = {8, 4, 2, 1};
const int Shift = CHAR_BIT * sizeof(long long);
const long long Mask = 1LL << (Shift - 1);
int x = 0; // index of vHex
for (int i = 1; i <= Shift; ++i) {
cout << (d & Mask ? 1 : 0);
vHex[x] += lookupPow[(i-1)%4] * (d & Mask ? 1 : 0);
if (i % 4 == 0) {
++x;
cout << " ";
if (i % 32 == 0) cout << endl;
}
d <<= 1;
}
cout << endl;
for (size_t j = 0; j < vHex.size(); ++j) cout << lookupHex[vHex[j]];
cout << endl;
}
4. By Sololearn
Made by Sololearn. Source
10011100
#include <iostream>
using namespace std;
/* A binary number is made up of elements called bits where each bit can be 1 or 0 */
string binary(int);
int main()
{
//sample number
int number = 156;
cout << binary(number);
return 0;
}
string binary(int num) {
string bin;
char holder=' ';
while(num != 0)
{
holder = num %2 + '0';
bin = holder + bin;
num /= 2;
}
return bin;
}
5. By DONALSOFT
Made by DONALSOFT. Source
Enter a decimal integer to convert it to octal and binary Input number is = 13 It's Octal equivalent is = 15 It's binary equivalent is = 1101
#include <iostream>
using namespace std;
int main() {
/***************************DONALSOFT**************************/
/******************DECIMAL TO OCTAL AND BINARY******************/
long num, decimal_num, remainder, base = 1, octal = 0, binary = 0;
long bdecimal_num, b_remainder, b_base = 1;
cout<<"Enter a decimal integer to convert it to octal and binary "<<endl;
cin>>num;
bdecimal_num = num;
decimal_num = num;
while(num > 0){
remainder = num % 8;
octal = octal + remainder * base;
num = num / 8;
base = base * 10;
}
while(bdecimal_num > 0){
b_remainder = bdecimal_num % 2;
binary = binary + b_remainder * b_base;
bdecimal_num = bdecimal_num / 2;
b_base = b_base * 10;
}
cout<<"Input number is = "<<decimal_num<<endl;
cout<<""<<endl;
cout<<"It's Octal equivalent is = "<<octal<<endl;
cout<<"It's binary equivalent is = "<<binary<<endl;
return 0;
}
6. By Janusz Bujak
Made by Janusz Bujak. Source
14 -> 1110
#include <iostream>
using namespace std;
long bin(long n) {
return n>0 ? n%2+10*bin(n/2) : 0;
}
int main() {
long n;
cin >> n;
cout << n << " -> " << bin(n);
}
/*
cout << bin << 999;
cout <<" oct "<< oct << 999;
cout <<" dec "<< dec << 999;
cout <<" hex "<< hex << 999;
*/
7. By Arnav
Made by Arnav. Source
Enter a number:15 Binary number: 1111
#include <iostream>
using namespace std;
int main() {
cout << " Enter a number: ";
int n;
cin >> n;
string b = "";
do
{
if (n % 2 == 1)
b = '1' + b;
if (n % 2 == 0)
b = '0' + b;
n /= 2;
}while (n != 0);
cout << endl << " Binary number: " << b;
return 0;
}
8. By RJ
Made by RJ. Source
Enter the number to convert 16 Binary of the given number=10000
#include <iostream>
using namespace std;
int main()
{
int a[10],n,i;
cout<<"Enter the number to convert";
cin>>n;
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
cout<<"Binary of the given number=";
for(i=i-1;i>=0;i--)
{
cout<<a[i];
}
return 0;
}
9. By AliR૯za
Made by AliR૯za. Source
17 17 in binary : (10001)
#include <iostream>
using namespace std;
int main()
{
int bin[1000],num,x;
cin>>num;
cout<<num;
for(;;x++)
{
bin[x]=num%2;
num/=2;
if(num==1)break;
}
cout<<" in binary : ("<<1;
for(;x>=0;x--)cout<<bin[x];
cout<<")";
}
10. By ApidBoy
Made by ApidBoy. This C++ Program Simply Converts Decimal Numbers Into Binary, STEPS TO USE: Simply Just Enter The Number And Run The Code You’ll Get Its Binary Conversion! Source
18 The Binary Conversion Of The Number 18 Is: 10010
#include<iostream>
using namespace std;
int main()
{
int arr[15];
int n;
cin>>n;
cout<<"The Binary Conversion Of The Number "<<n<<" Is: ";
int j=0;
while(n>0){
arr[j++]=n%2;
n=n/2;
}
for(int i=j-1;i>=0;i--){
cout<<arr[i];
}
}
11. By Kinshuk Vasisht
Made by Kinshuk Vasisht. Source
Number Entered - 19 Memory Representation - 00000000000000000000000000010011 Binary Representation - 10011
//Input - Any decimal number .
//Output - Their binary representations.
#include <iostream>
// cout, cin
#include <string>
// string
#include <cmath> // floor, ceil
#include <bitset>
// Header containing a class to store binary
// representations of types (anything, even
// classes),using a vector<bool>.
// Each bool variable stores the state of
// the bit, whether its a 1 or a 0.
#include <typeinfo>
// Header containing data on types. Useful
// for type comparison at compile time.
#define sub substr
using namespace std;
bool isint(float a)
// Checks if a is an int.
{
return (floor(a)==ceil(a));
}
string fw(string num)
// Erases extra zeroes from the beginning.
{
num.erase(0,min(num.find_first_not_of
('0'),num.size()-1));
return num;
}
string rv(string num)
// Erases extra zeroes from the end.
{
num.erase(num.find_last_not_of('0')+1,
num.size()-1); return num;
}
template<typename T>
string convert(T a)
/// Function to convert number to binary.
{
bitset<(sizeof(T)*8)> bin
= *(reinterpret_cast<int*>(&a));
// The bitset object is created by
// specifying the size of bits you need
// to see the number completely.
// Since we are working with bits, the
// best size will be the size ofthat
// class, multiplied by 8.
// (1 byte = 8 bits)
// The reinterpret_cast converts
// the variable to the form in which
// it is stored in memory. This is used
// to create the binary representation.
string res = bin.to_string();
/// We then convert the binary number to
/// a std::string for use.
return res;
}
int revert(string bin, int bias = 0)
/// Converts a binary string to decimal.
{
int a = stoi(bin,nullptr,2);
a -= bias;
return a;
}
template<typename T>
void Print(T num)
///Utility to Print Binary Representation.
{
cout<<"Number Entered - \n"
<<num<<endl<<endl;
if( typeid(T)==typeid(float) ||
typeid(T)==typeid(double) ||
typeid(T)==typeid(long double))
// If we received a float as the argument
{
int exp=0,man=0,bias=0,exv;
if(typeid(T)==typeid(float))
{exp = 8; bias = 127;}
if(typeid(T)==typeid(double))
{exp = 11; bias = 1023;}
if(typeid(T)==typeid(long double))
{exp = 15; bias = 16383;}
man = (sizeof(T)*8)-exp-1;
string res = convert(num),mant;
exv = revert(res.substr(1,exp),bias);
cout<<"Memory Representation - \n";
cout<<res<<endl<<endl;
// The above part of the code
// decides the number of bits to
// count for mantissa and exponent,
// which are combined with a sign bit
// to form the IEEE754 representation
// of floating-point numbers.
// The number of bits vary for float,
// double, and long double.
// The bias was added to exponents to
// keep the exponent > 0.
cout<<"Binary Representation - \n";
mant = "1"+res.sub(exp+1,man);
if(exv>0) mant = mant.sub(0,exv
+1)+"."+mant.sub(exv+1,
mant.size()-exv-2);
else if(exv==0) mant = mant.sub
(0,1)+"."+mant.sub(1,mant.size()-1);
else
{
for(int i=0;i<abs(exv)-1;i++)
mant="0"+mant;
mant = "0."+mant;
}
cout<<(res[0]=='0'?"":"-")<<rv(mant);
// The binary form of the float is
// calculated by prefixing a 1 to the
// mantissa, and then moving the
// decimal point from the original
// position, before the mantissa,
// to the final position as indicated
// by the exponent.
cout<<"\n\nSign - "<<(res[0]=='0'
?"Positive (+)\n":"Negative (-)\n");
cout<<"Exponent - "<<res.sub(1,
exp)<<" (with a bias of "<<bias
<<")"<<endl;
cout<<"Mantissa - "
<<res.sub(exp+1,man)<<endl<<endl;
}
else
// Otherwise, continue for integer.
{
cout<<"Memory Representation - \n"
<<convert(num)<<endl<<endl;
cout<<"Binary Representation - \n"
<<(num<0?"-":"")<<fw(convert
(abs(num)))<<endl<<endl;
}
cout<<endl;
}
int main()
{
union{int v2; float v1;} data;
float num; cin>>num;
if(cin.fail()) num = 26.2; // Default
if(!isint(num)) // Case of float.
{ data.v1=num; Print(data.v1); }
else // Case of int.
{ data.v2=num; Print(data.v2); }
}
12. By ROHIT KANOJIYA
Made by ROHIT KANOJIYA. Source
20 Binary Converstion of 20 : 10100
#include <iostream>
using namespace std;
/*
This Code Convert Decimal Number into Binary Number.
For Example:
Input: 10
Output:1010
*/
void DecimaltoBinary(int);
int main()
{
int num,n;
cin>>num;
cout<<"Binary Converstion of "<<num<<" : ";
n=num;
if(num<=0){
if(num==0)
cout<<"00000000";
else
cout<<"Enter Positive Number: ";
}
else
DecimaltoBinary(n);
return 0;
}
void DecimaltoBinary(int n)
{
int rem;
if(n==0)
return;
else{
rem=n%2;
DecimaltoBinary(n/2);
cout<<rem;
}
}
13. By RozeS
Made by RozeS. Source
DECIMAL :: 21 BINARY :: 10101 *****************************
#include <iostream>
using namespace std;
//🌷🌷🌷 Enter any decimal Number 🌷🌷🌷//
int main() {
long n , dec_n , rem , base=1 , bin=0;
//cout<<"enter a decimal\n\n";
cin>>n;
dec_n=n;
while(n>0){
rem=n%2;
bin = bin+rem*base;
n = n/2;
base = base*10;
}
cout<<"DECIMAL :: "<<dec_n<<"\n";
cout<<"BINARY :: "<<bin<<"\n";
cout<<"*****************************\n";
cout<<"Thank You :)";
return 0;
}
14. dρlυѕρlυѕ
Made by dρlυѕρlυѕ. Type in a decimal number and it will be converted to binary. Source
Decimal number: 22 Binary number: 0000 0000 0001 0110
#include <iostream>
using namespace std;
int main()
{
short dec;
bool tmp;
cout << "Decimal number: ";
cin >> dec;
cout << "\nBinary number: ";
for (int i=1; i<=16; i++)
{
tmp= (dec&0x8000) !=0;
cout << tmp ;
dec <<=1;
if (i%4==0)
cout << " ";
}
cout << endl;
return 0;
}
15. By Saksham Saxena
Made by Saksham Saxena. Source
Please enter only two digit number Enter the decimal to be converted:23 The binary of the given number is:10111
#include <iostream>
using namespace std;
int main() {
cout << " This converter is made by Saksham Saxena \n ";
cout << " Please enter only two digit number \n \n \n";
long dec,rem,i=1,sum=0;
cin>>dec;
cout<<"Enter the decimal to be converted:"<< dec<<endl;
do { rem=dec%2; sum=sum + (i*rem);
dec=dec/2; i=i*10;
}
while(dec>0);
cout<<"The binary of the given number is:"<<sum<<endl;
cin.get();
cin.get();
cout << "Please upvote if you like it !!! \n \nTHANK YOU :) ";
return 0;
}
16. By Shimon
Made by Shimon. Source
24 11000
#include <iostream>
using namespace std;
long long int B(long long int N){
long long int A;
if(N < 1){
return 0;
}
A = N % 2;
B(N / 2);
cout << A;
}
int main() {
long long int D;
cin >> D;
B(D);
return 0;
}
17. By Rishu Kumar
Made by Rishu Kumar. Enter the number to be converted in binary form as input Source
Number entered: 25 Binary form : 11001
#include <iostream>
using namespace std;
int main()
{
int x;
cin>>x;
int a[100];
int r; int n=x;
int i=0; int d=0;
while(n!=0)
{
r=n%2;
a[i]=r;
n=n/2;
i++; d++;
}
i--;
int b[100];
int j=0;
while(j<d)
{
b[j]=a[i];
j++;
i--;
}
cout<<"Number entered: "<<x<<endl;
cout<<"Binary form : ";
i=0;
while(i<d)
{
cout<<b[i];
i++;
}
return 0;
}
18. By GTimo
Made by GTimo. Input: Any decimal number from 0 – 255 Output: The number converted to an 8 bit binary number. Source
26 00011010
#include <iostream>
#include<iomanip>
using namespace std;
int main() {
int d1,d2,d3,d4,d5,d6,d7,d8,num;
cin>>num;
d8 =num % 2;
num = num /2;
d7 =num % 2;
num = num /2;
d6 =num % 2;
num = num /2;
d5 =num % 2;
num = num /2;
d4 =num % 2;
num = num /2;
d3 =num % 2;
num = num /2;
d2 =num % 2;
num = num /2;
d1 =num % 2;
cout<<d1<<d2<<d3<<d4<<d5<<d6<<d7<<d8;
return 0;
}
19. By Eligijus Silkartas
Made by Eligijus Silkartas. Source
enter a non negative integer 27 11011
#include <iostream>
using namespace std;
int main() {
int dec;
cout << "enter a non negative integer\n";
cin >> dec;
if(cin.fail()) {
cout << "are you sure that's a number?";
return 1;
}
if(dec < 0) {
cout << "program does not work with negative numbers... yet!";
return 1;
}
// string for binary digits
string bin = "";
// check how many binary digits there will be
int i = 1;
while(dec >> i > 0)
i++;
// start forming string
while(dec != 0) {
if(dec - (1 << --i) >= 0) {
bin += '1';
dec -= (1 << i);
}
else {
bin += '0';
}
}
// add 0's for remaining digits if needed
while(i-- != 0)
bin += '0';
cout << bin;
return 0;
}
20. By kellouche dhiya
Made by kellouche dhiya. Source
the valeu of number is : 28 binary is : 011100
#include <iostream>
#include <math.h>
using namespace std;
// binary calcule function
unsigned long long int binary_calcule (int num) {
int j = 0;
unsigned long long int rusalt = 0;
if(num < 0)
num *= -1;
while(num) {
rusalt += (num % 2) * pow(10, j++);
num /= 2;
}
return rusalt;
}
// STDOUT message fuction
void message_result(unsigned long long int rusalt,int num) {
cout << "the valeu of number is : " << num << endl << endl << "binary is : " << (num >= 0 ? 0 : 1) << rusalt << endl << "the first bit on the left represente the number sign " << endl << "if 1 means the number is negative else the number is positive" <<endl << endl << (num >= 0 ? "in this case the number is positive" : "in this case the number is nigative");
}
// main function
int main() {
int long DecimalNumber;
cin >> DecimalNumber;
unsigned long long int sort {binary_calcule(DecimalNumber)};
message_result(sort, DecimalNumber);
return 0;
}
21. By SRUSHTI BUCH
Made by SRUSHTI BUCH. Source
Enter Decimal Number 29 For Given Decimal 29 The Binary Form is: 11101
#include <iostream>
using namespace std;
int main() {
int d,a,i,b[10],l=0;
cout<<"Enter Decimal Number"<<endl;
cin>>d;
a=d;
for(i=0; (d!=(d%2)); i++){
b[i]=(d%2);
d=(d/2);
l++;
}
if(a==0)
b[i]=0;
else
b[i] =1;
cout<<"For Given Decimal "<<a<<" The Binary Form is: "<<endl;
for(i=l;i>=0;i--){
cout<<b[i];
}
return 0;
}
22. Shivangi Gupta
Made by Shivangi Gupta. Source
Enter the number : 30 The binary of 30 is 11110
#include<iostream>
using namespace std;
void bin(int n)
{
int rem;
if (n<= 1)
{
cout << n;
return;
}
rem = n % 2;
bin(n/ 2);
cout << rem;
}
int main()
{
int d;
cout << "Enter the number : \n";
cin >> d;
if (d < 0)
cout << "Enter the positive number"<< endl;
else
{
cout << "The binary of " << d<< " is ";
bin(d);
}
return 0;
}
23. By Amar Dahake
Made by Amar Dahake. Source
31 11111
#include <iostream>
using namespace std;
int main() {
int num;
int bin[100];
int i=0;
cin>>num;
while(num)
{
bin[i]=num%2;
num=num/2;
i++;
}
i -=1;
while(i>=0)
{
cout<<bin[i];
i--;
}
return 0;
}
24. By Vaneela Khatri
Made by Vaneela Khatri. Source
32 100000
#include <iostream>
using namespace std;
int main() {
int dec;
cin>>dec;
unsigned int bi;
int base=1,rem=0;
while (dec){
rem=dec%2;
bi+=rem*base;
dec/=2;
base*=10;
}
cout << bi;
return 0;
}
25. By Carlos Castro
Made by Carlos Castro. Source
Enter a decimal integer Input number is=33 Its binary equivalent is=100001
#include <iostream>
using namespace std;
int main() {
long num, decimal_num, remainder, base = 1, binary = 0;
cout<<"Enter a decimal integer\n";
cin>>num;
decimal_num = num;
while(num>0) {
remainder = num % 2;
binary = binary + remainder * base;
num = num / 2;
base = base * 10;
}
cout<<"Input number is="<<decimal_num<<"\n";cout<<"Its binary equivalent is="<<binary<<"\n";
return 0;
}
26. By Med Arezki
Made by Med Arezki.Basic Decimal to Binary Program. Source
34 to binary :100010
#include <iostream>
using namespace std;
int main()
{
int n(0),nC,a(0),m(1),nR(0);cin>>n;nC=n;
do{a=nC%2;nR+=a*m;m*=10;nC/=2;}while(nC!=0);
cout<<n<<" to binary :"<<nR<<endl;
//Nice challenge
return 0;
}
27. By Kai
Made by Kai. C++ Program for Decimal to Binary Conversion. Source
input no. is =35 its binary digit is =100011
#include <iostream>
using namespace std;
int main() {
long n,dn,r,b=1,binary=0;
cin>>n;
dn=n;
while (n>0){
r=n%2;
binary=binary+r*b;
n=n/2;
b=b*10;
}
cout <<"input no. is ="<<dn<<endl;
cout <<"its binary digit is ="<<binary;
return 0;
}
28. By Victor
Made by Victor. Source
enter a no:36 100100
#include <iostream>
using namespace std;
//Compiler version g++ 6.3.0
void binary(int num)
{
int rem;
if (num <= 1)
{
cout << num;
return;
}
rem = num % 2;
binary(num / 2);
cout << rem;
}
int main()
{
int b;
cout<<"enter a no:";
cin>>b;
binary(b);
return 0;
}
29. By STRIKER
Made by STRIKER. C++ Decimal to Binary Converter using Recursive. Source
Enter a decimal number 37 Binary equivalent of 37 is 100101
#include <iostream>
using namespace std;
int binary(int n){
if (n>1)
binary(n/2);
cout<<n%2;
}
int main() {
int decimal;
cout <<"Enter a decimal number";
cin>>decimal;
cout <<"Binary equivalent of "<<decimal<<" is ";
binary(decimal);
return 0;
}
30. By Sachin
Made by Sachin. Source
38 Binary equivalent is : 1 0 0 1 1 0
#include<iostream>
using namespace std;
int main()
{
double n,s[100];
int flag=0,i=0,m,a[100],k,j;
cin>>n; m=n;
do{ a[i++]=k=m%2;
}while(m=(m/2));
cout<<"Binary equivalent is : ";
for(i--;i>=0;i--) cout<<a[i]<<" ";
k=i=0;
m=n;
do{
i++;
n-=m;
if(!n) break;
else
{ if(i==1) cout<<". "; }
n*=2;
for(j=0;j<i-1;j++) if(s[j]==n) flag=1;
s[k++]=m=n;
cout<<m<<" ";
}while(flag!=1);
return 0;
}
31. By Kawaii
Made by Kawaii. Source
Enter the decimal number 39 The number in binary is --> 00100111
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Enter the decimal number" << endl;
int number, b;
string binary = "";
string rbinary;
cin >> number;
while (binary.length() < 8) {
b = number % 2;
number /= 2;
binary += to_string(b);
}
for (int i = binary.length(); i >= 0; i--) {
rbinary += binary[i];
}
cout << "The number in binary is --> " << rbinary << endl;
return 0;
}
32. By Wave Stuff
Made by Wave Stuff. Set to handle decimals from 0 to 65,535. If you want larger numbers just change the len variable to your desired length. Source
Decimal: 40 Binary : 0000000000101000
#include <iostream>
#include <bitset>
#include <string>
using std::bitset;
const unsigned len = 16;
int main() {
unsigned long d;
std::cin >> d;
std::cout << "Decimal: " << d << "\n";
std::string b = bitset<len>(d).to_string();
std::cout << "Binary : " << b;
return 0;
}
33. By TerrIA
Made by TerrIA. Source
41 001010
#include <iostream>
using namespace std;
int main() {
int bin,r[1000],q,i=0,j,k;
cin >> q;
bin = q;
for (k = 0; bin > 0; k++){
bin = bin/2;
r[i] = bin%2;
i++;
}
for(j = 0; j < i; j++){
cout << r[j];
}
}
34. By Imtinan Abbas
Made by Imtinan Abbas. Source
Enter the number: 42 The binary form of 42 is 101010
#include<iostream>
using namespace std;
void binary(int num)
{
int rem;
if (num <= 1)
{
cout << num;
return;
}
rem = num % 2;
binary(num / 2);
cout << rem;
}
int main()
{
int dec, bin;
cout << "Enter the number : ";
cin >> dec;
if (dec < 0)
cout << dec << " is not a positive integer." << endl;
else
{
cout << "The binary form of " << dec << " is ";
binary(dec);
cout << endl;
}
return 0;
}
35. By pur80a
Made by pur80a. Source
please insert your favorite number decimal:43 binary:101011
/*this code converts decimal into binary numbers
INPUT: Your favorite number
feel free to like*/
#include <iostream>
#include <vector>
using namespace std;
int main() {
int dec,bin;
cout<<"\t~<<DIGITALIZER>>~\n";
cout<<"converts decimal number into binary\n\n";
cout<<"please insert your favorite number\n";
cin>>dec;
//dec=11;
int help{dec}; //dec copied to help variable
vector<int> v; //maybe string would have been better
while(help!=0){
v.insert(v.begin(),help%2); //writes 1or0 into vector
help/=2; // equivalent to help=help/2
}
cout<<"decimal:"<<dec<<endl;
cout<<"binary:";
for(int i=0; i<v.size();i++){ //prints vector
cout<<v.at(i);
}
return 0;
}
36. By Bibaswan Roy
Made by Bibaswan Roy. Source
44 The binary expression of 44 is 101100
#include <iostream>
using namespace std;
int main() {
int num, i = 1, j;
long long int bina = 0;
cin >> num;//enter the positive integer
int c = num;
if (num < 0){cout << "Please enter a positive integer";}
else{
while (num > 0){
j = num % 2;
bina = bina + (j * i);
i *= 10;
num /= 2;
}
cout << "The binary expression of " << c <<" is "<<bina;}
return 0;
}
37. By HBKarachi
Made by HBKarachi. Source
Enter a decimal number: Step 1: 45/2, Remainder = 1, Quotient = 22 Step 2: 22/2, Remainder = 0, Quotient = 11 Step 3: 11/2, Remainder = 1, Quotient = 5 Step 4: 5/2, Remainder = 1, Quotient = 2 Step 5: 2/2, Remainder = 0, Quotient = 1 Step 6: 1/2, Remainder = 1, Quotient = 0 45 in decimal = 101101 in binary
#include <iostream>
#include <cmath>
using namespace std;
long long convertDecimalToBinary(int);
int main()
{
int n, binaryNumber;
cout << "Enter a decimal number: ";
cin >> n;
binaryNumber = convertDecimalToBinary(n);
cout << n << " in decimal = " << binaryNumber << " in binary" << endl ;
return 0;
}
long long convertDecimalToBinary(int n)
{
long long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (n!=0)
{
remainder = n%2;
cout << "Step " << step++ << ": " << n << "/2, Remainder = " << remainder << ", Quotient = " << n/2 << endl;
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
38. By Silver Key
Made by Silver Key. Source
plz enter decimal no.: 46 46 in binary = 0 0 0 0 1 0 1 1 1 0
/*
this program convert a decimal number
in range 0 to 1023 .. into binary :)
*/
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num,n,arr[]={0,0,0,0,0,0,0,0,0,0};
cout <<"plz enter decimal no.: \n\n";
cin >> num;
n = num;
for (int i=9; i>=0; i--)
{
if(pow(2,i) <= num)
{
num = num - pow(2,i);
arr[i] = 1;
}
}
cout << n << " in binary = ";
for (int i=9; i>=0; i--)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
39. By Khalid Mojahid
Made by Khalid Mojahid. Convert a number from decimal to binary using c++. Source
Enter a decimal number: 47 47 in decimal = 101111 in binary
#include <iostream>
#include<cmath>
using namespace std;
long long convert(int n);
int main() {
long long n;
cout<<"Enter a decimal number: "<<endl;
cin>>n;
cout<<n<<" in decimal = "<<convert(n)<<" in binary ";
return 0; }
long long convert(int n) {
long long bin = 0;
int rem, i = 1;
while (n != 0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10; }
return bin; }
40. By Med Arezki
Made by Med Arezki. Decimal To Binary (For just numbers from 0 to 15 (four digits). Source
Enter your number :10 Your number in bits :1010
#include <iostream>
using namespace std;
int main()
{
int a(0),b,c,d,e;
cout<<"Enter your number :";cin>>a;
cout<<"Your number in bits :";
b=a/2;
c=b/2;
d=c/2;
e=d/2;
cout<<d%2<<c%2<<b%2<<a%2;
return 0;
}
41. By Rameshwor Nepal
Made by Rameshwor Nepal. Source
Enter a decimal number: 48 Your binary number: 1 1 0 0 0 0
#include <iostream>
#include <stdlib.h>
using namespace std;
int a1, a2, remainder;
int tab = 0;
int maxtab = 0;
int table[0];
int main()
{
system("clear");
cout << "Enter a decimal number: ";
cin >> a1;
a2 = a1; //we need our number for later on so we save it in another variable
while (a1!=0) //dividing by two until we hit 0
{
remainder = a1%2; //getting a remainder - decimal number(1 or 0)
a1 = a1/2; //dividing our number by two
maxtab++; //+1 to max elements of the table
}
maxtab--; //-1 to max elements of the table (when dividing finishes it adds 1 additional elemnt that we don't want and it's equal to 0)
a1 = a2; //we must do calculations one more time so we're gatting back our original number
table[0] = table[maxtab]; //we set the number of elements in our table to maxtab (we don't get 10's of 0's)
while (a1!=0) //same calculations 2nd time but adding every 1 or 0 (remainder) to separate element in table
{
remainder = a1%2; //getting a remainder
a1 = a1/2; //dividing by 2
table[tab] = remainder; //adding 0 or 1 to an element
tab++; //tab (element count) increases by 1 so next remainder is saved in another element
}
tab--; //same as with maxtab--
cout << "Your binary number: ";
while (tab>=0) //until we get to the 0 (1st) element of the table
{
cout << table[tab] << " "; //write the value of an element (0 or 1)
tab--; //decreasing by 1 so we show 0's and 1's FROM THE BACK (correct way)
}
cout << endl;
return 0;
}
42. By Adam Staes
Made by Adam Staes. Source
10 1010
#include <iostream>
#include <list>
using namespace std;
int main()
{
int n;
list<bool> bin;
cin >> n;
do{
bin.push_front((n % 2));
n = (n - n % 2) / 2;
} while (n > 0);
for (bool x : bin)
{
cout << (x?1:0);
}
cout << endl;
return 0;
}
43. By harshini
Made by harshini. Source
enter decimal number 11 in binary :1011
#include <iostream>
using namespace std;
int main()
{
int rem[20];
int n,i,j;
cout<<"enter decimal number";
cin>>n;
for(i=0;n!=0;i++)
{
rem[i]=n%2;
n=n/2;
}
cout<<"\n"<< " in binary :";
for(j=i-1;j>=0;j--)
{
cout<<rem[j];
}
return 0;
}
44. By Aarti Rani
Made by Aarti Rani. Basic C++ Decimal to Binary program. Source
enter the decimal to change in binary:12 the binary number is:1100
#include <iostream>
using namespace std;
int decimaltobinary(int n)
{
int x=1;
int ans=0;
while(x<=n)
{
x*=2;
}
x/=2;
while(x>0)
{
int lastdigit=n/x;
n-=lastdigit*x;
x/=2;
ans=ans*10+lastdigit;
}
return ans;
}
int main() {
int n;
cout<<"enter the decimal to change in binary:";
cin>>n;
cout<<n<<endl;
cout<<"the binary number is:";
cout<<decimaltobinary(n)<<endl;
return 0;
}