Octal to Decimal Conversion

Octal to Decimal Conversion” is one of the classic programming problem exercise. Here, we are given an Octal Number, entered by user and our task is to write a program to convert the given Octal number to its equivalent Decimal Number.

Formula(Octal to Decimal Conversion): 
Let Octal number have ‘n’ digits.
F(n) = (Last Digit)*pow(8,0) + (Second last digit)*pow(8,1) + … + (first digit)*(8,n-1).

Example (Octal to Decimal Conversion):
Decimal = 116
Octal = 78
Decimal = 12
Octal 10

The steps required for Octal to Decimal Conversion is as follows:

  1. Let iterator = 0 and Decimal = 0
  2. Store the remainder when the Octal number is divided by 10.
  3. Decimal = Decimal + remainder * pow(8,iterator).
  4. Increment the iterator by 1.
  5. Divide the octal number by 10.
  6. Repeat the steps from 2 to 5 until the octal number is greater than 0.
  7. Print the result.

C++ Program for Octal to Decimal Conversion is as follows:

/* C++ Program for Octal to Decimal Conversion */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan the Octal Number */  
    int Octal;  
    cout<<"Enter the Octal Number: ";  
    cin>>Octal;  
      
    /* Converting Octal to Decimal*/  
    int Decimal = 0;  
    int iterator = 0;  
    int temp = Octal;  
    while(temp!=0)  
    {  
        int remainder = temp % 10;  
        Decimal = Decimal + remainder * pow(8,iterator);  
        iterator++;  
        temp = temp / 10;  
    }  
      
    /*Printing the result */  
    cout<<"The Decimal of "<<Octal<<" is "<<Decimal;  
}  

OUTPUT:
Enter the Octal Number: 12
The Decimal of 12 is 10

Related Posts:

  1. Program to convert octal number to binary number.
  2. Program to convert octal number to hexadecimal number.
  3. Program to convert decimal number to binary number.
  4. Program to convert decimal number to octal number.
  5. Program to convert decimal number to hexadecimal number.
  6. Program to convert hexadecimal number to binary number.
  7. Program to convert hexadecimal number to octal number.
  8. Program to convert hexadecimal number to decimal number.
  9. Program to convert binary number to octal number.
  10. Program to convert binary number to decimal number.
  11. Program to convert binary number to hexadecimal number.
  12. Program to find Quotient and Remainder.
  13. Program to find largest amongst three numbers.
  14. Program to find factorial of a number.
  15. Program to find GCD of two numbers.
  16. Program to find LCM of two numbers.
  17. Program to check whether entered number is odd or even.
  18. Program to check whether entered number is prime number or not.
  19. Program to check whether entered number is palindrome or not.
  20. Program to check whether entered number is Armstrong Number or Not.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *