Program of Number Conversions from Binary, Octal, HexaDecimal, Decimal, 1s Complement, 2s Complement with Menu Driven Options in C++ with UDF



//Program to Convert the Various Number Systems with 1s & 2s complements in C++

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<math.h>

void complement1()
{
char num[50];
int b[50],i;
clrscr();
cout<<"Enter Binary Number :";
cin>>num;
int len=strlen(num);
for(i=0;num[i]!='\0';i++)
{
b[i]=((int)num[i])-48;
}
for(i=0;i<len;i++)
{
if(b[i]==0)
{
b[i]=1;
}
else
{
b[i]=0;
}
}
cout<<"1's Complement is of Entered Binary number is :";
for(i=0;i<len;i++)
{
cout<<b[i];
}
}

void complement2()
{
char num[50];
int b[50],i,sum=0;
clrscr();
cout<<"Enter Binary Number :";
cin>>num;
int len=strlen(num);
for(i=0;num[i]!='\0';i++)
{
b[i]=((int)num[i])-48;
}
for(i=0;i<len;i++)
{
if(b[i]==0)
{
b[i]=1;
}
else
{
b[i]=0;
}
}
for(i=0;i<len;i++)
{
sum+=(pow(2,i))*b[i];
}
sum+=1;
int count=0,temp;
while(sum>0)
{
temp=sum%2;
b[count++]=temp;
sum=sum/2;
}
for(i=count-1;i>=0;i--)
{
cout<<b[i];
}
}

void BinToDec()
{
int a[20],i,j,temp,flag=0,sum=0;
long num;
clrscr();
cout<<"Enter Binary Number :";
cin>>num;
i=0;
while(num)
{
temp=num%10;
num=num/10;
flag++;
a[i]=temp;
i++;
}
for(i=0;i<flag;i++)
{
sum+=(pow(2,i)*a[i]);
}
cout<<"Decimal Number of Entered Binary is :"<<sum;
}

void BinToOct()
{
int a[20],octal[20],i,j,temp,flag=0,sum=0,o=0;
long num;
clrscr();
cout<<"Enter Number :";
cin>>num;
i=0;
while(num)
{
temp=num%10;
num=num/10;
flag++;
a[i]=temp;
i++;
}
for(i=0;i<flag;i++)
{
sum+=(pow(2,i)*a[i]);
}
i=0;
while(sum)
{
temp=sum%8;
sum=sum/8;
o++;
octal[i]=temp;
i++;
}
cout<<endl<<"Octal Number is :";
for(i=o-1;i>=0;i--)
{
cout<<octal[i];
}
}

void BinToHex()
{
char hex[20],num[50];
int temp,sum,i,j,stop,h=0;
cout<<"Enter Number :";
cin>>num;
}

void OctToDec()
{
char num[30];
int i,sum=0,temp=0;
clrscr();
cout<<"Enter Octal Number :";
cin>>num;
int len=strlen(num);
for(i=len-1;i>=0;i--)
{
sum+=(pow(8,temp))*(((int)num[i])-48);
temp++;
}
cout<<"Decimal Number of given Octal number is "<<sum;
}

void OctToBin()
{
char num[30];
int i,sum=0,temp=0,bin[20];
clrscr();
cout<<"Enter Octal Number :";
cin>>num;
int len=strlen(num);
for(i=len-1;i>=0;i--)
{
sum+=(pow(8,temp))*(((int)num[i])-48);
temp++;
}
i=0;
temp=0;
while(sum>0)
{
temp=sum%2;
bin[i++]=temp;
sum=sum/2;
}
cout<<"Binary of Given Octal Number is :";
for(int j=i-1;j>=0;j--)
{
cout<<bin[j];
}
}

void DecToBin()
{
int num,temp,i=0,bin[20];
clrscr();
cout<<"Enter Decimal Number :";
cin>>num;
while(num>0)
{
temp=num%2;
bin[i++]=temp;
num=num/2;
}
cout<<"Binary Number of Given Decimal Number :";
for(int j=i-1;j>=0;j--)
{
cout<<bin[j];
}
}

void DecToOct()
{
int num,temp,i=0,octal[20];
clrscr();
cout<<"Enter Decimal Number :";
cin>>num;
while(num>0)
{
temp=num%8;
num=num/8;
octal[i]=temp;
i++;
}
cout<<"Octal Number of Entered Decimal Number is ";
for(int j=i-1;j>=0;j--)
{
cout<<octal[j];
}
}

void DecToHex()
{
int num,temp,i=0;
char hexa[20];
clrscr();
cout<<"Enter Decimal Number :";
cin>>num;
while(num>0)
{
temp=num%16;
num=num/16;
if(temp==10)
{
hexa[i]='A';
}
else if(temp==11)
{
hexa[i]='B';
}
else if(temp==12)
{
hexa[i]='C';
}
else if(temp==13)
{
hexa[i]='D';
}
else if(temp==14)
{
hexa[i]='E';
}
else if(temp==15)
{
hexa[i]='F';
}
else
{
hexa[i]=(char(temp))+48;
}
i++;
}
hexa[i]='\0';
int len=strlen(hexa);
cout<<"Octal Number of Entered Decimal Number is ";
for(i=len-1;i>=0;i--)
{
cout<<hexa[i];
}
}

void HexToDec()
{
char num[20];
int i,sum=0,temp=0;
cout<<"Enter HexaDecimal Number :";
cin>>num;
int len=strlen(num);
for(i=len-1;i>=0;i--)
{
if(num[i]=='A')
sum+=(pow(16,temp))*10;
else if(num[i]=='B')
sum+=(pow(16,temp))*11;
else if(num[i]=='C')
sum+=(pow(16,temp))*12;
else if(num[i]=='D')
sum+=(pow(16,temp))*13;
else if(num[i]=='E')
sum+=(pow(16,temp))*14;
else if(num[i]=='F')
sum+=(pow(16,temp))*15;
else
{
sum+=(pow(16,temp))*(((int)num[i])-48);
}
temp++;
}
cout<<"Decimal Number of Entered HexaDecimal Number is :"<<sum;
}

void HexToBin()
{
char num[20];
int i,sum=0,temp=0,bin[30];
cout<<"Enter HexaDecimal Number :";
cin>>num;
int len=strlen(num);
for(i=len-1;i>=0;i--)
{
if(num[i]=='A')
sum+=(pow(16,temp))*10;
else if(num[i]=='B')
sum+=(pow(16,temp))*11;
else if(num[i]=='C')
sum+=(pow(16,temp))*12;
else if(num[i]=='D')
sum+=(pow(16,temp))*13;
else if(num[i]=='E')
sum+=(pow(16,temp))*14;
else if(num[i]=='F')
sum+=(pow(16,temp))*15;
else
{
sum+=(pow(16,temp))*(((int)num[i])-48);
}
temp++;
}
temp=0;
i=0;
while(sum>0)
{
temp=sum%2;
bin[i++]=temp;
sum=sum/2;
}
cout<<"Binary Number of Entered HexaDecimal Number is :";
for(int j=i-1;j>=0;j--)
{
cout<<bin[j];
}
}

void HexToOct()
{
char hex[20],c;
int n;
cout<<"Enter Hexadecimal Number : ";
cin>>hex;
int i,len, dec=0, oct=0;
for(len=0; hex[len]!='\0'; len++);
for(i=0; hex[i]!='\0'; i++,len--)
{
if(hex[i]>='0' && hex[i]<='9')
{
dec= dec + (hex[i]-'0')*pow(16,len-1);
}
if(hex[i]>='A' && hex[i]<='F')
{
dec = dec + (hex[i]-55)*pow(16,len-1);
}
if(hex[i]>='a' && hex[i]<='f')
{
dec = dec + (hex[i]-87)*pow(16,len-1);
}

i=1;
while(dec!=0)
{
oct = oct + (dec%8)*i;
dec = dec/8;
i = i*10;
}
cout<<"Octal Number of Entered Hexadecimal Number is :"<<oct;
}

void menu()
{
clrscr();
cout<<"==============================="<<endl;
cout<<" MENU OF NUMBER CONVERSIONS    "<<endl;
cout<<"==============================="<<endl;
cout<<"0.  Exit"<<endl;
cout<<"1.  1's Complement"<<endl;
cout<<"2.  2's Complement"<<endl;
cout<<"3.  Binary to Decimal"<<endl;
cout<<"4.  Binary to Octal"<<endl;
cout<<"5.  Binary to HexaDecimal"<<endl;
cout<<"6.  Octal to Decimal"<<endl;
cout<<"7.  Octal to Binary"<<endl;
cout<<"9.  Decimal to Binary"<<endl;
cout<<"10. Decimal to Octal"<<endl;
cout<<"11. Decimal to HexaDecimal"<<endl;
cout<<"12. HexaDecimal to Decimal"<<endl;
cout<<"13. HexaDecimal to Binary"<<endl;
cout<<"14. HexaDecimal to Octal"<<endl;
cout<<"=============================="<<endl;
cout<<"Enter Your Choice :";
}

void main()
{
int ch;
do
{
menu();
cin>>ch;

switch(ch)
{
case 0: exit(0);
case 1: complement1(); break;
case 2: complement2(); break;
case 3: BinToDec(); break;
case 4: BinToOct(); break;
case 5: BinToHex(); break;
case 6: OctToDec(); break;
case 7: OctToBin(); break;
case 9: DecToBin(); break;
case 10: DecToOct(); break;
case 11: DecToHex(); break;
case 12: HexToDec(); break;
case 13: HexToBin(); break;
case 14: HexToOct(); break;
default:
cout<<"Wrong Input";
}
getch();
}while(ch>0);
}

No comments

Post your comments

Powered by Blogger.