C++ Program to convert decimal to octal using a user-defined function


February 1, 2023, Learn eTutorial
1032

In this C++ program, we have to convert a decimal number into its octal number using a function.

Decimal and Octal number

  • A decimal number is a number that has its base as 10. which means, we have to express a number using 0 to 9.
  • The octal number is a number system that has its base 8. That means it has a total of 8 digits to denote a number, 0 to 7

Decimal to Octal conversion

To convert decimal to octal, we have to divide the decimal number by the base of the octal number system, which is 8. Let’s take an example to understand it clearly. 204 is taken as the decimal number which we want to convert into its octal number. Here we have to first divide the number 204 by 8, we have got 25 as the result and 4 as the remainder, and now again divide 25 by 8. Continue dividing until the number becomes less than that number through with you dividing, that is 8 here.
204/8 Q= 25, R= 4;
25/8 Q=3, R= 1;
After dividing the number or after getting a remainder less than 8, stop dividing and write the remainder from last to first. Here we have, 314 as a result or octal value of the same number given in decimal number, that is 204.

How to write a C++ program to convert a number from decimal to octal?

Ask the user to enter a decimal number. Get the number into the variable decimalNum; Define a function to calculate the octal number corresponding to the entered decimal number.

void DecimalToOctal(int);

Here the function takes the decimal number as its argument and converts it into its equivalent octal value.


void DecimalToOctal(int decimalNum)
{
   while(decimalNum != 0)
   {
      octalNum[i] = decimalNum%8;
      i++;
      decimalNum = decimalNum/8;
   }
}


When the function gets invoked, the condition of the while loop gets evaluated. That is, the condition, decimalNum!=0  evaluates to be true, therefore program flow goes inside the loop. There, decimalNum%8 gets initialized to octalNum[i]. The value of i gets incremented. And decimalNum/8 gets initialized to decimalNum. Now program control goes back and evaluates the while loop condition again with the new value of variable decimalNum. Continue the process of evaluating the while loop until its condition evaluates to be false. we've to print the value of octalNum[] from its last to 0th index. So using for loop, we've printed the value, starting from i-1 to the 0th index of octalNum[ ].

Algorithm

Step 1: Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Declare a user-defined function void DecimalToOctal(int);

Step 4: Open the integer type main function; int main().

Step 5: Declare integer type variable decimalNum;

Step 6: Ask the user to enter a decimal number

Step 7:Get the number into the variable decimalNum;

Step 8: invokes the function DecimalToOctal(int decimalNum)
             Find the % of the decimalNum and store the remainder within an array octalNum[i];
             Update the value of decimalNum by placing decimalNum/8 and continue.

Step 9: Print the value that the function returns

Step 10::Exit

C++ Source Code

                                          #include<iostream>
using namespace std;
void DecimalToOctal(int);
int octalNum[50];
static int i;
int main()
{
    int decimalNum;
    cout<<"Enter any Decimal number: ";
    cin>>decimalNum;
    DecimalToOctal(decimalNum);
    cout<<"\nEquivalent Octal Value = ";
    for(i=(i-1); i>=0; i--)
        cout<<octalNum[i];
    cout<<endl;
    return 0;
}
void DecimalToOctal(int decimalNum)
{
    while(decimalNum != 0)
    {
        octalNum[i] = decimalNum%8;
        i++;
        decimalNum = decimalNum/8;
    }
}
                                      

OUTPUT

Enter any Decimal number: 365
Equivalent Octal Value = 555