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): 
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:
Decimal = 116
Octal = 78
Decimal = 12
Octal 10

The steps required to convert an Octal Number to Decimal Number 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 to Convert Octal to Decimal is as follows:

/* C++ Program to Convert Octal Number to Decimal Number */  
#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 sum of first ‘n’ natural numbers.
  13. Program to Reverse a Number.
  14. Program to swap two Numbers.
  15. Program to print Fibonacci Series up to Nth Term.
  16. Program to find and print nth Fibonacci number.
  17. Program to find Power of the Number.
  18. Program to find Quotient and Remainder.
  19. Program to check Leap Year.

You may also like...

Leave a Reply

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