You are on page 1of 4

#include <iostream.h> #include <math.

h> int main() { while (true) { int binaryValue, c, tempOctal, tempDecimal, octalValue = 0, e; cout << "Enter a binary number to convert to octal: "; cin >> binaryValue; int counter = ceil(log10(binaryValue+1)); cout << "Counter " << counter << endl; int iter; if (counter%3 == 0) { iter = counter/3; } else if (counter%3 != 0) { iter = (counter/3)+1; } cout << "Iterations " << iter << endl; c = binaryValue; cout << "C " << c << endl; for (int h = 0; h < iter; h++) { tempOctal = c%1000; cout << "3 digit binary part " << tempOctal << endl; int count = ceil(log10(tempOctal+1)); cout << "Digits " << count << endl; tempDecimal = 0; for (int counterr = 0; counterr < count; counterr++) { if (tempOctal%10 != 0) { e = pow(2.0, counterr); tempDecimal += e; cout << "Temp Decimal value 0-7 " << tempDecimal << endl; } tempOctal /= 10;

} octalValue += (tempDecimal * pow(10.0, h)); cout << "Octal Value " << octalValue << endl; c /= 1000; } cout << "Final Octal Value: " << octalValue << endl; } system("pause"); return 0;

------------------------------#include <math.h> #include <iostream.h> #include <string.h> #include <ctype.h> #include <stdlib.h>

char* toBinary(char* doubleDigit) { int digit = atoi(doubleDigit); char* binary = new char(); int x = 0 ; binary[x]='('; //int tempDigit = digit; int k=1; for(int i = 9 ; digit != 0; i--) { k=1;//cout << digit << endl; //cout << "i"<< i<<endl; if(digit-k *pow(8,i)>=0) { k =1; cout << "i" << i << endl; cout << k*pow(8,i)<< endl; while((k*pow(8,i)<=digit))

{ k++; } k= k-1; digit = digit -k*pow(8,i); binary[x+1]= k+'0'; binary[x+2]= '*'; binary[x+3]= '8'; binary[x+4]='^'; binary[x+5]=i+'0'; binary[x+6]='+'; x+=6; } } binary[x]=')'; return binary; } int main() { char value[6]={'4','0','9','8','7','9'}; cout<< toBinary(value); return 0 ; }

You might also like