C++ Convert decimal to octal in printf
Posted on 2003-02-20
Here is my code. Does anyone know a simple way to convert the sum in printf statement to an octal value?
Any help would be appreciated.
int octal, decimal = 0;
int number1, number2, sum;
int highBit = 512 /*8^3*/, factor = 1000 /*10^3*/;
printf( "Enter two numbers ( no more than 4 digits ) in base 8: " );
scanf( "%d%d", &number1, &number2, &octal );
while ( highBit >= 1 )
decimal += octal / factor * highBit;
highBit /= 8;
octal %= factor;
factor /= 10;
sum = number1 + number2;
printf( "Result: %d + %d = %d\n", number1, number2, sum );