×

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

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

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.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

Counting Frequencies of Array Elements in C++

We have an array of integer items with duplicate values, and our objective is to compute the frequencies of the different elements in the array. Methods: Methods that can be used to...

3 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

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.

Function overloading in C++

Function overloading in C++ As we know that C++ works on the OOP Concepts, that are abstraction, encapsulation, and data hiding, it also uses the other important feature of OOP, which...

8 minutes read.

How to create a table in C++

C++ is a programming language that has evolved as an improvement of c language. It includes an object-oriented archetype. C++ programming language is a compiled language that complies with the...

3 minutes read.