Decimal to Octal in C++
We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of 10 to a number with a base value of 8. The number of digits used to represent a numeric value is determined by the base value of a number system.
Example of a program to convert decimal to octal in C++:
// C++ program to convert a decimal
// number to octal number
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
using namespace std ;
// function to convert decimal to octal
void decimalToOctal ( int n )
{
// array to store octal number
int octalNumber [ 100 ] ;
// counter for octal number array
int i = 0 ;
while ( n != 0 ) {
// storing remainder in octal array
octalNumber [ i ] = n % 8 ;
n = n / 8 ;
i++ ;
}
// printing octal number array in reverse order
for ( int j = i - 1 ; j >= 0 ; j-- )
cout << octalNumber [ j ] ;
}
// Driver Code
int main ( )
{
int n = 16 ;
// Function Call
decimalToOctal ( n ) ;
return 0;
}
OUTPUT:
20
…..
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
- When 16 is divided by 8, the remainder is 0. As a result, arr [ 0 ] = 0.
- 16 divided by 8 equals 16 / 8 = 2 is the new number.
- When 2 is divided by 8, the remainder is 2. As a result, arr [ 1 ] = 2.
- 2 divided by 8, 2 / 8 = 0 is the new number.
- Since the number is equal to zero,
- Stop repeating the steps and print the array backwards. As a result, the octal number equivalent is 20.
- Time Complexity will be 0 ( logn ).
Let us see another example of a code with 0 ( 1 ) space complexity in C++:
Let us see another example of a code with 0 ( 1 ) space complexity in C++:
// C++ program to convert decimal
// number to octal number
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
using namespace std ;
// function to calculate the octal value of the given
// decimal number
void decimaltoOctal ( int decimalNumber )
{
// initializations
int octalNumber = 0 , countvalue = 1 ;
int dNo = decimalNumber ;
while ( decimalNumber != 0 ) {
// decimals remainder is calculated
int remainder = decimalNumber % 8 ;
// storing the octalvalue
octalNumber += remainder * countval ;
// storing exponential value
countvalue = countvalue * 10 ;
decimalNumber /= 8 ;
}
cout << octalNumber << endl ;
}
// Driver Code
int main ( )
{
int n = 16 ;
// Function Call
decimaltoOctal ( n ) ;
return 0 ;
}
OUTPUT:
20
….
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
- Set the octal num to 0 and the countValue to 1, as well as the decimal number to n.
- When a decimal number is divided by 8, get the remaining.
- Updating by octalNumber + (remainder * countvalue)
- Countvalue should be multiplied by countvalue * 10.
- 8 times a decimal number
- Continue from step 2 until the decimal number reaches zero.
Let us look at another example of a program in C++ using pre defined function:
#include < bits/stdc++.h >
#include < iostream >
#include < stdlib >
using namespace std ;
string intToOctalnum ( int n )
{
stringstream st ;
st << octn << n ;
return st.str ( ) ;
}
int main ( )
{
int n = 16 ;
cout << intToOctalnum ( n ) ;
return 0 ;
}
OUTPUT:
20
….
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
In the above program, we have made a string function and named it as intToOctalnum and passing the decimal number as the parameter and in the main function we are making a call to function intToOctalnum.
Another example of pre defined function without using string:
#include <iostream>
using namespace std;
long decimalToOctalnum ( long n );
int main ( ) {
long decimalnum ;
cout << "enter a decimal number\n";
cin >> decimalnum;
cout << "Octal number : " << decimalToOctalnum ( decimalnum ) ;
return 0;
}
long decimalToOctalnum ( long n ) {
int remainder ;
long octal = 0 , i = 1 ;
while ( n != 0 ) {
remainder = n % 8 ;
n = n / 8 ;
octal = octal + (remainder*i);
i = i*10;
}
return octal;
}
OUTPUT:
Enter a decimal number 16
Octal number: 20
………………………………………
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
In the C++ program above, we accept an integer from the user and store it in the variable decimalnum. Then, using the above-mentioned procedure, we call the decimalToOctalnum function to convert the decimal function to an octal number.