Created
June 21, 2018 04:29
-
-
Save aligalehban/270eeef36179aa72efe0120cd980be24 to your computer and use it in GitHub Desktop.
Convert Binary Number to Octal and vice-versa in c++ source code - www.alighalehban.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int convertBinarytoOctal(long long); | |
int main() | |
{ | |
long long binaryNumber; | |
cout << "Enter a binary number: "; | |
cin >> binaryNumber; | |
cout << binaryNumber << " in binary = " << convertBinarytoOctal(binaryNumber) << " in octal "; | |
return 0; | |
} | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment