Octal to Binary number converter


#include < iostream.h >
void main ()
{
const int converts = 10;

for (int ctr = 1;ctr <= converts; ctr++)
{
int dec = 0, mult = 1, prod = 0, rem, oct, px, chk;
bool isoctal = true;

cout << "Enter a valid Octal Number: ";
cin >> oct;

px = oct;

while (px > 0)
{
chk = px % 10;
if (chk >= 8)
{
isoctal = false;
cout << "Invalid Octal Number\n\n";
break;
}
px = px / 10;
}

if (isoctal)
{
while (oct > 0)
{
rem = oct % 10;
prod = rem * mult;
dec = dec + prod;
mult = mult * 8;
oct = oct / 10;
}

mult = 1;

while (dec > 0)
{
rem = dec % 2;
prod = rem * mult;
oct = oct + prod;
mult = mult * 10;
dec = dec / 2;
}

cout << "The Binary equivalent is: " << oct << "\n\n";
}

}
}


source: http://www.planetsourcecode.com/

0 comments:

Post a Comment