×

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input.

Let us look at an example of a program in C++ to do so:

// C++ program to convert octal to decimal
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
using namespace std ; 
// Function to convert octal to decimal
int octalToDecimalnumber ( int n )
{
	int number = n ;
	int decimal_value = 0 ; 
	// Initializing base value to 1, i.e 8 ^ 0
	int base_val = 1 ; 
	int temp = number ;
	while ( temp ) {
		// extracting last digit
		int last_digit = temp % 10 ;
		temp = temp / 10 ; 
		// Multiplying last digit with appropriate
		// base value and adding it to decimal_value
		decimal_value += last_digit * base ; 
		base = base * 8 ;
	}
	return decimal_value ;
}
// Driver program to test above function
int main ( )
{
	int number = 33 ; 
	cout << octalToDecimalnumber ( number ) << endl ;
}

OUTPUT:

27
…..
Process executed in 0.11 seconds
Press any key to continue.

Explanation:

The goal is to subtract the digits of the octal number starting at the far right digit and keep them in a variable called decimal value. Multiply the digit by the correct base (8 Power) and add the variable decimal value when subtracting the octal numerical digits. Finally, the decimal number will be saved on a variable decimal value.

Another example of a program using pre-define function:

// C++ program to convert octal to decimal
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
using namespace std ;
int OctalToDecimal ( string n )
{
return stoi ( n, 0, 8 ) ;
}
int main ( )
{
string n = "33" ;
cout << OctimalToDecimal ( n ) ;
return 0 ;
}

OUTPUT:

27
…..
Process executed in 0.22 seconds
Press any key to continue.

Explanation:

In the above program in C++, we are using stoi function in 7th line of code which takes the octal number and divides it with 8 to convert it into decimal number.


Related Topics

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

goto statement in C and C++

goto statement in C and C++ The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used...

3 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

Do-While Loop Examples in C++

Before we move on the examples of Do-While loop, let’s learn little bit about the Do-While loop in C++ language. Do While loop An iterative loop that checks the condition at the...

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

DES in C++

The popularity of the Data Encryption Standard (DES) is slightly declining as a result of the discovery that DES is susceptible to very strong attacks. Since DES is a block cypher,...

3 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

Dynamic Constructor in C++

Dynamic constructors create dynamic memory by using a dynamic memory allocator new within the constructor. This allows us to initialize objects dynamically. The term dynamic constructor is used when memory...

2 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.