Program to read one number from the user and display its octal and hexadecimal equivalent.
#include<iostream>
#include<conio.h>
using namespace std;
int octal_conversion(int
number);
void hexadecimal_conversion(int
number);
int main()
{
int number = 0, octal_number = 0;
cout
<< "\n\t\t__ Octal and Hexadecimal
Conversion __";
cout
<< "\n\n\n Enter the Decimal Number - ";
cin
>> number;
octal_number
= octal_conversion(number);
cout
<< "\n\n\t __ Octal Conversion of "
<< number << " is "
<< octal_number << " __";
hexadecimal_conversion(number);
getch();
return 0;
}
int octal_conversion(int
number)
{
int remainder = 0, octal_number = 0, counter = 1;
while(number != 0)
{
remainder
= number % 8;
number
= number / 8;
octal_number
= octal_number + remainder * counter;
counter
= counter * 10;
}
return octal_number;
}
void hexadecimal_conversion(int
number)
{
int original_number = 0, remainder = 0, counter = 0,
i = 0, value = 0;
char hexadecimal_number[10];
original_number
= number;
while(number > 0)
{
remainder
= number % 16;
number
= number / 16;
value
= 0;
if (remainder >= 0 && remainder <= 9)
{
value
= 48;
for ( i = 1; i <= remainder; i ++)
{
value
= value + 1;
}
}
else if (remainder
>= 10 && remainder <= 15)
{
value
= 65;
for ( i = 11; i <= remainder; i ++)
{
value
= value + 1;
}
}
hexadecimal_number[counter]
= (char)value;
counter++;
}
cout
<< "\n\n\t __ Hexadecimal Conversion of
" << original_number << "
is ";
for( i =
counter - 1 ; i >= 0; i -- )
{
cout
<< hexadecimal_number[i];
}
cout
<< " __";
}
can u add a binary please? i need it ASAP ..thx 4 granting ^^
ReplyDelete