×

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

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

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.

Loops in C++

A loop statement in most programming languages allows us to execute a statement or a collection of statements numerous times. Control structures of programming languages vary, allowing for more complex...

6 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

C++ Continue in Do-while loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.

Multiple Inheritance

C++ Multiple Inheritance Multiple inheritance is such an inheritance in which a derived class inherits properties of more than one base class.     C++ Multiple Inheritance Example In this example, two base classes Square and...

2 minutes read.

Difference between Two Sets in C++

The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the...

3 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Diamond Pattern in C++ using For Loop

For Loop: A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times. The syntax of for loop: In C++, a for...

5 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

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

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

Stringstream in C++ and its applications

In this tutorial, we will explore what the stringstream in C++ is. We will also learn its application. What is stringstream? With the aid of a stringstream, user can read from a...

2 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer...

3 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.