×

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.


Related Topics

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

Maps in C++

Maps: Maps in C++ are the containers associated with key and mapped values. By keys and mapped values, we mean that the maps are used to store elements formed by the...

4 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

Print Table Using For-loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

3 minutes read.

C++ Heap Sort

Heapsort is executed on the structure of the heap data. We know heap is a complete tree in binary form. The heap tree can be of two different types: Min-heap,...

3 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

Array of Object in C++

What is an Array? In C++, an array is a group of related data types, such as int, char, float, double, etc., that are easily accessed by index value alone and...

12 minutes read.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

C++ Continue

In C++, the continue statement is a useful tool for avoiding specific scenarios without breaking the loop. It is employed inside loops to move directly to the following iteration and...

4 minutes read.

Difference between C and C++

What do you mean by C? C is a machine-independent structure or procedural oriented computer language that is widely utilized in a variety of applications. C is a fundamental programming language...

4 minutes read.

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...

3 minutes read.