This is a C++ Program to Convert a Given Octal Number to its Decimal Equivalent.
The program takes an octal number and converts it into its decimal equivalent.
1. The program takes an octal number.
2. Using a while loop, each digit is converted to its decimal equivalent by multiplying with powers of 8.
3. The result is printed.
4. Exit.
Here is the source code of C++ Program to Convert a Given Octal Number to its Decimal Equivalent. The program output is shown below.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num, temp, rem, oct = 0, i = 0;
cout << "Enter an octal number : ";
cin >> num;
temp = num;
while(temp != 0)
{
rem = temp % 10;
oct += rem * pow(8, i++);
temp = temp / 10;
}
cout << "\nDecimal equivalent of " << num << " is : " << oct;
return 0;
}
1. The user is asked to enter an octal number and it is stored in the variable ‘num’.
2. num is copied to a temporary variable ‘temp’. The variables ‘oct’ and ‘i’ are initialized as 0.
3. Using a while loop, the remainder of temp on dividing by 10 is stored in ‘rem’.
4. rem is multiplied with powers of 8 using the function pow() and is added to oct. i is incremented in the function.
5. temp is divided by 10.
6. The loop continues till temp is not equal to 0.
7. The result is then printed.
Case 1 : Enter an octal number : 135 Decimal equivalent of 135 is : 93 Case 2 : Enter an octal number : 7 Decimal equivalent of 7 is : 7 Case 3 : Enter an octal number : 25 Decimal equivalent of 25 is : 21
Sanfoundry Global Education & Learning Series – C++ Programs.
To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.
- Practice Programming MCQs
- Apply for C++ Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship