C++ program to convert a decimal value to octal

C++ program to convert a decimal value to octal:

In this post, we will learn how to convert a decimal value to octal in C++. It will take one decimal value as input from the user, convert it to octal and print it out.

Both octal and decimal are different number systems. The decimal number uses base 10 and the octal number uses base 8. For decimal, we use numbers from 0 to 9 and for the octal number system, we have to use numbers from 0 to 7.

Algorithm to convert a decimal value to octal:

We can convert a decimal value to octal with a simple algorithm:

  • Take the decimal value as input from the user.
  • If the number is 7 or less than 7, then the octal value is the same for this number.
  • If it is greater than 7, then,
    • Divide the number by 8. Save the remainder.
    • Take the quotient and repeat the above step. We have to repeat it till the quotient becomes 0.
    • If we join the remainders of the divisions in reverse, it will give the octal representation of the given decimal number.

Let’s take an example:

Number Quotient of ÷8 Remainder of ÷8
1231 153 7
153 19 1
19 2 3
2 0 2

This example is finding the octal value of 1231. If we combine the remainders in reverse, it will be 2317 i.e. this is the octal representation of 1231.

Example 1: C++ program to convert a decimal value to octal by using while loop:

The below C++ program converts a decimal value to octal:

#include <iostream>
using namespace std;

int toOctal(int decimal)
{
    int octal = 0;
    int multiplier = 1;
    int remainder;

    while (decimal != 0)
    {
        remainder = decimal % 8;
        octal += remainder * multiplier;
        decimal /= 8;
        multiplier *= 10;
    }
    return octal;
}

int main()
{
    int decimal, octal;

    cout << "Enter the decimal value:" << endl;
    cin >> decimal;

    octal = toOctal(decimal);

    cout << "Octal: " << octal << endl;
}

Here,

  • toOctal method is used to convert a decimal value to octal. It takes the decimal number as its parameter, converts it to octal and returns that.
    • It uses a while loop to do the conversion.
    • The octal variable is initialized as 0. It is used to hold the final octal value.
    • The multiplier variable is initialized as 1. It is multiplied with the remainder to add to the octal. It is multiple of 10 so that the remainder is added to the start of octal.
    • The while loop runs until the value of decimal is not equal to 0.
    • It finds the remainder and adds it to the start of the octal. It multiplies the remainder by the multiplier variable before adding to octal so that it is added to the front.
    • The decimal value is changed on each iteration and also the multiplier is multiplied by 10 at the end of each iteration.
  • Once the loop ends, it returns the calculated octal value.
  • The main function is taking the decimal value as input from the user and calls the toOctal function to convert it to octal. It prints the calculated octal at the end of the function.

If you run this program, it will print output as below:

Enter the decimal value:
1231
Octal: 2317

Example 2: C++ program to convert a decimal value to octal by using for loop:

We can also use for loops to do the above conversion.

#include <iostream>
using namespace std;

int toOctal(int decimal)
{
    int octal = 0;
    int remainder;

    for (int multiplier = 1; decimal != 0; multiplier *= 10)
    {
        remainder = decimal % 8;
        octal += remainder * multiplier;
        decimal /= 8;
    }
    return octal;
}

int main()
{
    int decimal, octal;

    cout << "Enter the decimal value:" << endl;
    cin >> decimal;

    octal = toOctal(decimal);

    cout << "Octal: " << octal << endl;
}

In this example,

  • The for loop calculates the octal value. It is similar to the previous example. On each iteration, it creates the octal result.

It will print similar results if you run the program.

C++ convert decimal to octal