×

Division in C++

C++ Division Arithmetic Operation

In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right operand Using sample programs; in this tutorial you will learn how to use the arithmetic division operator with values ??of various data types.

Syntax

result = operand_1 / operand_2

 Attempts to split operand_1 into equal parts, operand_2. The dividend is operand_1 and the divisor is operand_2.

 The precision may or may not be stored in the result, depending on the data types of the operands andresult.

C++ Division by Two Integers

#include <iostream>
using namespace std;
int main() {
   int a = 16;
   int b = 7;
   int div = a / b;
cout<< div <<endl;
}

 The division operator can divide two integers. The following code snippet shows the data types of the operands and the return value. Since both operands are integers,  if the dividend is not exactly divisible by the divisor, the division operator only returns the quotient and discards memory. The following program initializes two integer variables and then uses them as operands of a division operator.

Output:

2

C++ Division Using Two Floating Point Numbers

 You can divide two floating point numbers using the division operator. The following code snippet shows the data types of the operands and the return value. The division operator divides the dividend by the divisor until the full floating-point precisionis reached because both the dividend and the divisor are floating-point numbers.

#include <iostream>
using namespace std;
int main() {
   float a = 21.3;
   float b = 4.1;
   float div = a / b;
cout<< div <<endl;
}

Output:

5.19512

C++ Division by Integers and Floats

 You can use integers for floating-point division. The following code snippet shows the data types of the operands and the return value. float = int/floatThe following program initializes an integer and a float variable, then divides and stores the float variable. Accuracy is preserved. However, precision is lost when the result is stored as an integer.

#include <iostream>
using namespace std;
int main() {
   int a = 16;
   float b = 7.1;
   float div = a / b;
cout<< div <<endl;
}

Chaining Division Operators

 To divide three or more operands in one statement, you can chain division operators. The fake code is shown below

  • Result = Operand_1 / Operand_2 / Operand_3 /.. / operand_n

 The following example uses the arithmetic division operator to divide all four integer variables in one statement. Calculations are performed from left to right. So the first step is to do operand_1 / operand_2 and divide the result.

#include <iostream>
using namespace std;
int main() {
   int a = 100;
   int b = 2;
   int c = 10;
   int div = a / b / c;
cout<< div <<endl;
}

Explicit Typecasting

Programmers do this explicitly. The static_cast keyword is used for explicit type casting.

Such a conversion is performed at compile time.

The explicit cast solves theinteger division problem in the following code. The m and n values ??are statically converted to a floating point in the code below on line 6; Therefore the result of the division is also  a floating point number

#include<iostream>
using namespace std;
int main() {
    int m = 10;
    int n = 7;
    float a = static_cast<float>(m)/ static_cast<float>(n); //static explicit casting
cout<< "The answer after explicit typecasting is: " <<a<<endl;
}

Output:

The answer after explicit typecasting is: 1.42857

Implicit Typecasting

The compiler automatically performs an implicit typecast. The expression is evaluated by the compiler based on the data types used in the expression

Alldata types used in a given expression are evaluated to the highest data type by the compiler.

The fundamental reason for theinteger divide function is the implicit cast. The calculation is implicitly converted to data type int when using two integer operands with arithmetic division, which truncates the decimal part of the result

As shown in the following code, we can change the type of a division operand to a floating point type to solve the integer division problem:

#include<iostream>
using namespace std;
int main() {
    float a = 10.0/7;
cout<< "The answer after implicit typecasting is: " <<a<<endl;

Output:

The answer after implicit typecasting is: 1.42857

Related Topics

Function Pointer in C++

Definition: A function pointer in C++ is the same as a usual pointer which is used to point some variables. Function pointers are used to point functions or say store the...

4 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

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

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

C++ Features

C++ is a general-purpose programming language that evolved from the C language to include an object-oriented paradigm. It is a compiled and imperative language. Object-Oriented Programming Object-oriented programming language concepts: ClassObjectsEncapsulationPolymorphismInheritanceAbstraction Class: A Class...

4 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

4 minutes read.

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

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

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

Advantage and disadvantage friend function C++

Friend Function: - A friend function in C++ is a function that can access the private, protected, and public members of a class. In C++, a friend function is a function that...

2 minutes read.