Sie sind auf Seite 1von 2

#include <iostream.

h>
#include <stdlib.h>
void To_Binary();
void To_Octal();
void To_Hexadecimal();
//void exit();
int main()
{

int choice;
char choose;
start:
cout<<"\nThis program will convert the decimal number to binary,octal &
hexadecimal.\n";
cout<<"What conversion do you want to execute this program:\n\n";
cout<<"The choices are between the range of '1' to '4' only:\n\n";
cout<<"\t 1 For Binary\n";
cout<<"\t 2 For Octal\n";
cout<<"\t 3 For Hexadecimal\n";
cout<<"\t 4 For Exit\n";
cin>>choice;

switch (choice)
{
case 1: To_Binary();
break;
case 2: To_Octal();
break;
case 3: To_Hexadecimal();
break;
case 4:
exit(0);
break;
}

cout<<"\nIf you want the program to continue just press 'Y' otherwise press
'N':\n\n";
cin>>choose;
if(choose=='Y'||choose=='y')
goto start;
else
exit(0);
cout<<endl<<endl;

system("PAUSE");
return 0;
}
//function to convert Decimal Number to Binary

void To_Binary()
{
int number,range[10],x;
cout<<"\nEnter any Decimal Number to convert its Binary equivalent:\n\n";
cin>>number;
cout<<"\nThe Binary equivalent of "<<number<<" is = ";
for(x=0;number!=0;x++)
{
range[x]=number%2;
number=number/2;
}
x--;
for(;x>=0;x--)
cout<<range[x];
cout<<endl<<endl;
}
//function to convert Decimal Number to Octal

void To_Octal()
{
int number,range[10],x;
cout<<"\nEnter any Decimal Number to convert its Octal equivalent:\n\n";
cin>>number;
cout<<"\nThe Octal equivalent of "<<number<<" is = ";
for(x=0;number!=0;x++)
{
range[x]=number%8;
number=number/8;
}
x--;
for(;x>=0;x--)
cout<<range[x];
cout<<endl<<endl;
}
//Function to convert Decimal Number to Hexadecimal

void To_Hexadecimal()
{
int number,range[10],x;
cout<<"\nEnter any Decimal Number to convert its Hexadecimal
equivalent:\n\n";
cin>>number;
cout<<"\nThe Hexadecimal equivalent of "<<number<<" is = ";
for(x=0;number!=0;x++)
{
range[x]=number%16;
number=number/16;
}
x--;
for(;x>=0;x--)

if(range[x]==10)
cout<<"A";
else if(range[x]==11)
cout<<"B";
else if(range[x]==12)
cout<<"C";
else if(range[x]==13)
cout<<"D";
else if(range[x]==14)
cout<<"E";
else if(range[x]==15)
cout<<"F";
else
cout<<range[x];
cout<<endl<<endl;
}

Das könnte Ihnen auch gefallen