Friday, 20 June 2014

C++ program to convert decimal to binary

Here is a C++ program to convert decimal to binary. Here are different methods to convert decimal to binary like using recursion, using bitwise operator,using function overloading etc. I have also discussed a method to convert floating point decimal number to binary.
How to convert decimal to binary (base 10 to base 2).
To convert decimal to binary a simple method is to continuously divide the number by 2 until final result is zero, in each division we either get one or zero simply wrote the remainders starting from bottom to upwards .For example let us convert decimal number 6 to binary.
6 / 2  quotient = 3 ,rem=0
3 / 2  quotient = 1 ,rem=1
1 / 2  quotient = 0 ,rem=1
Thus binary equivalent of decimal number 6 is 110.

C++ program to convert decimal to binary

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    long binary=0,rem,num,temp=1;
    cout<<"Enter the number ";
    cin>>num;
    while(num>0)
    {
        rem=num%2;
        num/=2;
        binary=binary+rem*temp;
        temp*=10;
    }
    cout<<"Binary equivalent : "<<binary;
    return 0;
}



C++ program to convert decimal to binary using functions


#include<iostream>
using namespace std;
void bin(int);
int main()
{
    int n;
    cout<<"Enter number : ";
    cin>>n;
    cout<<"Binary equivalent : ";
    bin(n);
    return 0;
}
void bin(int n)
{
    int a[20],i;
    for(i=0;n!=0;i++)
    {
        a[i]=n%2;
        n=n/2;
    }
    for(int y=i-1;y>=0;y--)
        cout<<a[y];
}



C++ program to convert decimal to binary using recursion


#include<iostream>
using namespace std;
void binary(int n)
{
    if(n==0)
        return ;
    binary(n/2);
    cout<<n%2;
}
int main()
{
    int num;
    cout<<"Enter Decimal number : ";
    cin>>num;
    cout<<"Binary equivalent : ";
    binary(num);
    return 0;
}



C++ program to convert decimal to binary using bitwise operator


#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
    int a,amask,y,num,k;
    cout<<"Enter the decimal number ";
    cin>>a;
    cout<<"Binary equivalent ";
    num=a;
    for(y=0;num>0;y++,num/=2);
 
    for(int i=y-1;i>=0;i--)
    {
        amask=1<<i;
        k=a&amask;
        k==0?cout<<"0":cout<<"1";
    }
}



C++ program to convert floating point decimal number to binary

#include<iostream>
using namespace std;
void bin(int n)
{
    if(n==0)
        return;
    bin(n/2);
    cout<<n%2;
}
int main()
{
    int a;
    float n;
    cout<<"Decimal equivalent : ";
    cin>>n;
    a=n;
    cout<<"Binary equivalent : ";
    bin(n);
    cout<<".";
    while(n!=0)
    {
        n-=a;
        n=n*2;
        a=n;
        cout<<a;
    }
    return 0;
}



C++ program to convert decimal number to binary using function overloading


#include<iostream>
using namespace std;
void bin(int n)
{
    if(n==0)
        return;
    bin(n/2);
    cout<<n%2;
}
void convert(int n)
{
    bin(n);
}
void convert(float n)
{
    int a=n;
    bin(n);
    cout<<".";
    while(n!=0)
    {
        n-=a;
        n=n*2;
        a=n;
        cout<<a;
    }
}
int main()
{
    cout<<"Binary equivalent of 45     : ";
    convert(45);
    cout<<endl<<"Binary equivalent of 54.135 : ";
    convert(54.135F);
    return 0;
}



C++ program to convert decimal to binary and store it as string


#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char a[20];
    int i,n;
    cout<<"Decimal equivalent : ";
    cin>>n;
    for(i=0;n>0;i++)
    {
        a[i]=n%2+48;
        n=n/2;
    }
    a[i]='\0';
    strrev(a);
    cout<<"Binary equivalent : "<<a;

}



1 comment: