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

In this program, we are going to learn to convert a decimal number into its octal number using a user-defined function.

Decimal and Octal number

The decimal number has its base as 10. That means it has a total of 10 digits, which are 0, 1, 2, ..., 8, and 9. Whereas, the octal number has its base as 8. That means it has a total of 8 digits, which are 0, 1, 2, ..., 6, 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 result and 4 as the remainder, and now again divide 25 with 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 the 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.

C++ program to convert a decimal number into its octal number by using a user-defined function

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 flow goes back and evaluates the condition of the while loop again with the new value of 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