codescracker
c++

C++ Program to Convert Octal to Decimal



« Previous Program Next Program »

In this article, you will learn and get code on octal to decimal conversion in C++. The octal to decimal conversion program is created with and without using function.

But before starting the program, if you're not aware about the simple formula and steps used for the conversion, then you can refer to octal to decimal formula to get every required things.

Octal to Decimal in C++

To convert octal number to decimal number in C++ programming, you have to ask from user to enter the octal number and then convert it into its equivalent decimal value. Print the equivalent decimal value on output as shown in the program given below.

The question is, write a program in C++ that converts octal number to decimal number. Here is its answer:

// C++ Program to Convert Octal to Decimal
// ----codescracker.com----

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int octalNum, decimalNum=0, i=0, rem;
    cout<<"Enter the Octal Number: ";
    cin>>octalNum;
    while(octalNum!=0)
    {
        rem = octalNum%10;
        decimalNum = decimalNum + (rem*pow(8,i));
        i++;
        octalNum = octalNum/10;
    }
    cout<<"\nEquivalent Decimal Value: "<<decimalNum;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program convert octal to decimal

Now supply the input as any octal number say 345 and press ENTER key to convert and print its equivalent decimal value as shown here in the snapshot (of sample output) given below:

octal to decimal c++

The dry run of above program with octal number input 345 goes like:

Without using pow() Function

To create the same program (as of previous one) without using pow() function, just replace the following statement:

decimalNum = decimalNum + (rem*pow(8,i));
i++;

with the statement given below:

decimalNum = decimalNum + (rem*mul);
mul = mul*8;

Note - Don't forgot to declare the variable mul at start of the program. And initialize its initial value as 1

Remove the declaration and initialization of variable i from the program. Also remove the header file, math.h. Rest of the things will be same.

Program to Deal with Invalid Octal Input

Octal number has its base as 8. Therefore it has total of 8 digits, that are from 0 to 7. Other digits like 8 and 9 is not a valid octal digit. Therefore if user enters a number that contains 8 or 9. Then here is the program to deal with:

// Program to Deal with Invalid Octal Digit
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int octalNum, rem, temp, chk=0;
    cout<<"Enter the Octal Number: ";
    cin>>octalNum;
    temp = octalNum;
    while(temp!=0)
    {
        rem = temp%10;
        if(rem>=8)
        {
            chk++;
            break;
        }
        temp = temp/10;
    }
    if(chk==0)
    {
        int decimalNum=0, mul=1;
        while(octalNum!=0)
        {
            rem = octalNum%10;
            decimalNum = decimalNum + (rem*mul);
            mul = mul*8;
            octalNum = octalNum/10;
        }
        cout<<"\nEquivalent Decimal Value: "<<decimalNum;
    }
    else
        cout<<"\nInvalid Octal Digit!";
    cout<<endl;
    return 0;
}

Here is its sample run with user input 349 as octal number. Because it contains 9 (the third digit of 349), and 9 is an invalid octal digit, therefore here is the output you will see:

c++ octal to decimal

Octal to Decimal using Function

This is the last program on octal to decimal conversion in C++. It is created using a user-defined function OctalToDecimal(). The function takes octal number as its argument, and returns either its equivalent decimal value or 0.

It returns 0, if there is any invalid octal digit present in the entered octal number, otherwise returns equivalent decimal value.

// Octal to Decimal using User-defined Function
// ----codescracker.com----

#include<iostream>
using namespace std;
int OctalToDecimal(int);
int main()
{
    int octalNum, decimalNum;
    cout<<"Enter the Octal Number: ";
    cin>>octalNum;
    decimalNum = OctalToDecimal(octalNum);
    if(decimalNum==0)
        cout<<"\nInvalid Octal Digit!";
    else
        cout<<"\nEquivalent Decimal Value: "<<decimalNum;
    cout<<endl;
    return 0;
}
int OctalToDecimal(int octalNum)
{
    int temp, rem, chk=0;
    temp = octalNum;
    while(temp!=0)
    {
        rem = temp%10;
        if(rem>=8)
        {
            chk++;
            break;
        }
        temp = temp/10;
    }
    if(chk==0)
    {
        int decimalNum=0, mul=1;
        while(octalNum!=0)
        {
            rem = octalNum%10;
            decimalNum = decimalNum + (rem*mul);
            mul = mul*8;
            octalNum = octalNum/10;
        }
        return decimalNum;
    }
    else
        return 0;
}

This program produces the same output as of previous program.

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker