thanks for helping me...
im using c++ language...
how can you convert decimal to binary..
should i divide it? can you give me a hint for what code should i use...thanks

The same way I showed you in your other thread, only now use an interger as input:

int a = 17;
std::cout << std::bitset<CHAR_BIT>( a ) << std::endl;

Will give 00010001 as output (17 binary)

Edited 6 Years Ago by Nick Evan: n/a

If you're looking to create a sequence of ones and zeros, you need to do some division and modulus operations.
Look here for some example of the process. You can do a 'net search and find many more examples.

#include<iostream>
using namespace std;
int bin(int num);
int main()
{
    cout<<"Enter a binary number ";
    int num;
    cin>>num;
    cout<<endl<<endl;
    bin(num);
    cout<<endl<<endl;
    system("PAUSE");
    return 0;
}
int bin(int num)
{
    int a[25];
    int i;
    i=0;
    int r;
    int d;
    int b;
    int j;
    j= 0; 
    cout<<"Binary is : ";      
    while(num>0)
    {
          r=num%2;
          a[j]=r;
          d=num/2;
          b=num;
          num=d;
          j++;
    }    
    while(j>=0)
    {
              cout<<a[j];
              j--;
    }
}

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

Comments
Bad code and 3 years late.
Don't bump old threads

Try dis.....

#include<iostream.h>
#include<malloc.h>
class d2b
{
 public:
int dec2bin(int *p)
{
 int a,b,c=1,d=0;
 while (*p!=0)
 {
  a = *p%2;
  b = a * c;
  d = d + b;
  *p = *p/2;
  c = c * 10;
 }
 return d;
}
};
void main()
{
 d2b db;
 int *n,x,y;
 n = (int*)malloc(sizeof(int));
 cout<<"Enter the decimal number: ";
 cin>>*n;
 y = *n;
 x = db.dec2bin(n);
 cout<<"The binary conversion of "<<y<<" is "<<x<<endl;
}

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

Comments
Did you read the post above?
#include<stdio.h>

int pow1(int,int);
void main(char* argv)
{   
    int i=0,n=0,j=0;
    int bin=0;
    printf("Enter a number \n");
    scanf("%d",&n);
    for(i=0;n>0;i++)
    {
        j=n%2;
        bin=bin+j * pow1(10,i);
        n/=2;       
    }
    printf("The binary number is %d \n",bin);
}

int pow1(int a , int b)
{
    int i=0;
    int sum=1;
    for(i=0;i<b;i++)
        sum*=a;

    return sum;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Comments
Erm sure...
int decimal,remender,sum=0,i=1;
cout<<"enter your integer and decimal number"<<endl;
cin>>decimal;
do{
    remender=decimal%2;
    sum=sum+(i*remender);
    decimal=decimal/2;
    i=i*10;
    }
    while(decimal>0)
    cout<<"the binary number is="<<sum<<endl;
Comments
SO you decided to ignore the last post?
#include<iostream.h>
#include<conio.h>

class conversion
{
private:
  int n,i,p,q;
  int arr[23],ar[23],a[23];
public:


        void input()

    {
            cout<<"how many binary nos you want to enter?   ";
        cin>>n;
         cout<<endl<<endl;
         p=0;
         n--;
        for(i=n;i>=0;i--)
        {cout<<"enter binary no    ";
        cin>>arr[i];
         p++;}
    }


    void func()

      {
      n=1;
      for(i=0;i<=p;i++)
      {ar[i]=n;
      n=n*2;}



    }


      void output()

        {  p--;
        for(int j=0;j<=p;j++)
         {
         a[j]=arr[j]*ar[j];
            }
         q=0;
         for(int j=0;j<=p;j++)
         q=q+a[j];
         cout<<endl<<endl;
         cout<<"decimal conversion "<<q;
        }
};


    void main()

    {
        conversion c;
         c.input();
         c.func();
         c.output();

        getch();
    }

enjoys guys

template<class _T>
const char * dec2bin(_T dec)
{
    static char szret[65];
    int ch = sizeof(dec) * 8;
    memset(szret, 0, ch+1);
    char * ptr = szret;
    for( int i=0;i<ch;i++ )
        ptr[ch-1-i] = '0' + (dec >> i & 1);
    return ptr;
}

I've tried before posting this. dec2bin function accepts any integer (BYTE, short, long, __int64). Hope this helps.

How bout dis???

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
   clrscr();
   int a[200],i=0,dec,bin,j,k;
   cout<<"Enter the number:\n";
   cin>>dec;
   int quotient=dec;
   do
   {  a[i]=quotient%2;
      i++;
      quotient=quotient/2;

   }  while(quotient!=0);
   int strlen=i;
      cout<<endl;
   for(i=strlen-1;i>=0;i--)
   {  cout<<a[i];
   }
  getch();
}
#include <iostream>

int main () {
    int b,m,n;
    std::string s;
    std::cout<<"Input decimal number.\nInput \"0\" = end.\n\n";
    do  {
        std::cout<<"dec: ";
        std::cin>>n;
        m=n;
        std::cout <<"hex: "<< std::hex << n << '\n';
        std::cout <<"oct: " << std::oct << n << '\n';
        while(n>0){
            s+=(n%2)+48;
            n/=2;
            }
        std::cout<<"bin: ";
        while(s.size()>0){ std::cout<<s[(s.size()-1)]; s=s.substr(0,(s.size()-1)); }
        std::cout<<"\n\n";
        s="";
        }
    while(m>0);
    return 0;
    }
This article has been dead for over six months. Start a new discussion instead.