Binary to Octal

    0

    0

    Giovanny Gongora

    Codiga's C++ Recipes

    Convert binary number to octal

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    int convertBinarytoOctal(long long);
    int convertBinarytoOctal(long long binaryNumber) {
      int octalNumber = 0, decimalNumber = 0, i = 0;
      while(binaryNumber != 0){
        decimalNumber += (binaryNumber%10) * pow(2,i);
        ++i;
        binaryNumber/=10;
      }
      i = 1;
      while (decimalNumber != 0){
        octalNumber += (decimalNumber % 8) * i;
        decimalNumber /= 8;
        i *= 10;
      }
      return octalNumber;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.