Skip to content

Instantly share code, notes, and snippets.

@aligalehban
Created June 21, 2018 04:29
Show Gist options
  • Save aligalehban/232555a3617d966bae9f41c8e6244ee5 to your computer and use it in GitHub Desktop.
Save aligalehban/232555a3617d966bae9f41c8e6244ee5 to your computer and use it in GitHub Desktop.
convert octal to decimal in c++ source code - www.alighalehban.com
#include <iostream>
#include <cmath>
using namespace std;
int octalToDecimal(int octalNumber);
int main()
{
int octalNumber;
cout << "Enter an octal number: ";
cin >> octalNumber;
cout << octalNumber << " in octal = " << octalToDecimal(octalNumber) << " in decimal";
return 0;
}
// Function to convert octal number to decimal
int octalToDecimal(int octalNumber)
{
int decimalNumber = 0, i = 0, rem;
while (octalNumber != 0)
{
rem = octalNumber % 10;
octalNumber /= 10;
decimalNumber += rem * pow(8, i);
++i;
}
return decimalNumber;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment