My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program to convert binary number into decimal number in c++

Wednesday, September 23, 2009

//binary to decimal conversion
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{       clrscr();
    int num[50];
    char ch,st[2];
    for(int i=0;i<50;i++)
    num[i]=-1;

    i=0;
    cout<<"enter the binary number: ";
    while((ch=getch())!='\r')
    {
        cout<<ch;
        st[0]=ch;
        st[1]='\0';
        num[i]=atoi(st);
        i++;
    }
    int max=0,sum=0,m=0;
    while(num[max]!=-1)
    max++;

    max--;
    cout<<endl<<endl;
    while(max>=0)
    {    sum=sum+(num[max]*pow(2,m));
        max--;m++;
    }
    cout<<endl<<sum;
    getch();

}

0 comments: