## What is a Division Operator?

We all learnt about division—and the division operator—in school. You probably know of both these symbols as representing division:

÷     and     /

Division in computer programming is more than finding a quotient and a remainder. There are different types of division, and different data types—which we’ll now learn about.

In computer programming, the division operator is a forward slash. The numbers or variables on either side of the slash are called the operands. In the expression “a/b”:

• a is the left operand
• the slash is the division operator
• b is the right operand

The division operator in a computer programming language is a symbol with a defined function. This operator tells the compiler or interpreter to perform a division operation. The operator divides the left operand by the right operand and gives only the quotient as the result.

## How does Division Operator Work?

In this article, we’ll learn about the division operator used in computer programming languages. Let’s first look at the basic types of division operations in computer programming:

To understand integer division and float division, we need to know about the various data types used in programming languages. The primary or fundamental data types are integer type, floating point type, character type and void.

Here, we will only look at integer types and floating-point types.

The integer data type allows the storage of whole numbers. There are six integer data types.

The size and range of each of these data types in a 16-bit processor are listed in the following table. Note that these values can vary among compilers:

The float data type allows the storage of real numbers. There are three float data types. The size and range of each of these data types in a 16-bit processor are listed in the following table. Note that, as with integer data types, these values can vary among compilers:

In integer division, both operands are of the integer data type. The division operation involves an integer type dividend divided by an integer type divisor to produce an integer type quotient.

Consider this division problem: You want to distribute 92 candy canes equally among eight children. How many candy canes can each child get? The answer is 11, even though there is a remainder when 92 is divided by 11.

That’s what integer division operator does; it only gives the quotient.

The following C++ code segment demonstrates this:

#include <iostream>

using namespace std;

int main()

{

`       `     int num1 = 37;

`         ` int num2 = 8;

`         ` int quotient = num1 / num2;

`         ` cout << quotient;

return 0;

}

This code, compiled using a standard C++ compiler, will result in 11 as the output.

In the above example, variables “a”, “b” and “c” are all of the integer data type. Here:

• “num1”—which we call the dividend—is the left operand for the division operation.
• “num2”—which we call the divisor—is the right operand for the division operation.
• The result of the operation gives a value, or quotient, that is stored in the variable “quotient.”

This is how we can write it in C language:

# include <stdio.h>

int main()

{

`      `int num1 = 92;

`      `int num2 = 8;

`      `int quotient = a / b;

`     ` printf ("%d",c);

`      `return 0;

}

“%d” and “%i” are format specifiers for the integer data type.

The division operator only gives the quotient, so you need a different operator to find the remainder. That operator is the modulus operator, denoted by the “%” symbol.

Replacing the division operator (/) with the modulus operator (%) in the above code segment as follows:

c = a % b;

will give 4 as the result of the modulus operation—which is the remainder of the division problem.

## Float Division

In float division, either or both of the operands are of the float data type. That is:

• a float type dividend is divided by an integer type divisor, or
• an integer type dividend is divided by a float type divisor, or
• a float type dividend is divided by a float type divisor to produce a float type quotient.

Consider the division problem of distributing \$36 equally among eight people. How much money does each person get? The answer is \$4.50.

That’s what float division does; it gives a quotient with a decimal point.

The following C++ code segment demonstrates this:

` `#include <iostream>

using namespace std;

int main()

{

`      `float num1 = 36;

`      `float num2 = 8;

`      `float result = num1 / num2;

`      `cout<<quotient;

`      `return 0;

}

This code, compiled using a standard C++ compiler, will result in 4.5 as the output.

In the above example, variables “num1”, “num2” and “result” are all of the float data type. Here:

• “num1”—which we call the dividend—is the left operand for the division operation.
• “num2”—which we call the divisor—is the right operand for the division operation.
• The result of the operation gives a value, that is stored in the variable “result.”

Lets' see how can we write it in C language:

#include <stdio.h>

int main()

{

float a = 36;

float b = 8;

float c = a / b;

printf ("%f",c);

return 0;

}

“%f” is the format specifier for the float data type.

Why is the output “4.500000” and not just “4.5”?

The reason is that the precision of the “float” data type—as listed in the table with the size and range of the three float data types—is up to six decimal points.

To get “4.5” as the output, you write “%.1f” in place of “%f” in the print f() statement.

## Precedence and Associativity of the Division Operator

When two operators with different precedence are adjacent to each other in an expression, which operator should be evaluated first is based on the operator precedence rule. The rule says the division operator has the same precedence as the multiplication and modulus operators. However, it has lower precedence than the unary operators—Postfix, Prefix, Bitwise NOT and Logical NOT.

When two operators with the same precedence are adjacent to each other in an expression, which operator should be evaluated first is based on associative rules—which tell the compiler whether it should evaluate from left to right (LR) or from right to left (RL). The associativity of the division operator is LR.

## Concept and Applications

This topic is significant for learning basics of programming and it is also important in the professional exams for undergraduate courses, especially for:

• B. Tech in Computer science
• B. Tech in Computer engineering
• B. Tech in Information Technology
• B. E in Computer science
• B. E in Computer engineering
• B. E in Information Technology
• Bachelor of Computer Applications (BCA)

### Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

### Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science