C++ Program for Octal to Decimal Conversion

Program for Octal to Decimal Conversion

The concept of converting an octal number to a decimal number is very simple , extract the digits of the octal number (start from right for convenience )and multiply the digits with the proper base(power of 8) lastly add each of the results and we will get a converted number as an output.

Example: 2012

Decimal number     = 2*(8^3) + 0*(8^2) + 1*(8^1) + 2*(8^0)= 1024 + 0 + 8 + 2

            = 1034

Program to Convert octal to decimal

Algorithm:-

  1. User gives an input(octal number)
  2. The input is stored in an int type variable say octal.
  3. A function convert()  is called with octal as parameter.
    1. Digits of octal are extracted one by one starting from right.

                     int rem = octal%10;

                2. Extracted digits are multiplied with proper base i.e. power of 8.

                     int res = rem*pow(8,i);

                3. After multiplying each digit with proper base the results are added and stored in another variable say decimal.

                    decimal += res;

     4. The output is printed.

C++ Code:-

    
    //C++ Program
    //Octal to decimal conversion
    #include <iostream>
    #include <math.h>
    using namespace std;
    // Function to convert octal number to decimal
    int convert(int octal)
    {
        int decimal = 0i = 0;
        //converting octal to decimal
        while (octal != 0)
        {
            int rem = octal % 10;
            octal /= 10;
            int res=rem*pow(8,i);
            decimal += res;
            i++;
        }
        return decimal;
    }
    //main program
    int main()
    {
        int octal;
        cout << “Enter an octal number: “;
        //user input
        cin >> octal;
        //calling function
        int decimal=convert(octal);
        //printing output
        cout << octal << ” in octal = “ << decimal << ” in decimal”;     
        return 0;
    }

    Output

    Enter an octal number: 2022
    2022 in octal = 1042 in decimal
coding (3)
  • Highest Common Factor(HCF): C | C++Java | Python
  • Lowest Common Multiple (LCM) : C | C++ | Java | Python
  • Greatest Common Divisor : C | C++ | Java | Python
  • Binary to Decimal to conversion : C | C++ | Java | Python
  • Binary to Octal conversion : C | C++ | Java  | Python
  • Decimal to Binary conversion: C | C++ | Java | Python
  • Decimal to octal Conversion: C | C++ | Java | Python
  • Octal to Binary conversion : C | C++ | Java | Python
  • Octal to Decimal conversion : C | C++ | Java | Python
  • Quadrants in which a given coordinate lies : C | C++ | Java | Python
  • Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
  • Maximum number of handshakes: C | C++ | Java | Python
  • Addition of two fractions: C | C++ | Java | Python
  • Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
  • Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
  • Count possible decoding of a given digit sequence : C | C++ | Java
  • Check whether a character is a vowel or consonant : C | C++ | Java | Python
  • Check whether a character is a alphabet or not : C | C++ | Java | Python
  • Calculate the area of a circle : C | C++ | Java | Python
  • Find the ASCII value of a character : C | C++ | Java | Python
  • Find the prime numbers between 1 to 100 : C | C++ | Java | Python
  • Calculate the number of digits in an integer : C | C++ | Java | Python
  • Convert digit/number to words : C | C++ | Java | Python
  • Counting number of days in a given month of a year: C | C++ | Java | Python
  • Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
  • Finding number of integers which has exactly x divisors: C | C++ | Java | Python
  • Finding Roots of a quadratic equation : C | C++ | Java | Python