Variables, Types and Operators

# How does integer division work in C++?

## Can you explain more about how integer division and the modulus operator work in C++, with some examples?

In C++, when you divide two integer values using the / operator, the result is also an integer. It performs what's called integer division.

The / operator returns the quotient - which is how many times the divisor goes into the dividend, rounded down.

For example:

#include <iostream>

int main() {
std::cout << 7 / 3;   // Outputs 2
std::cout << ", ";
std::cout << 10 / 3;  // Outputs 3
}
2, 3

The % operator returns the remainder after integer division. For example:

#include <iostream>

int main() {
// 7 / 3 has a remainder of 1
std::cout << 7 % 3;

std::cout << ", ";

// 10 / 3 has a remainder of 1
std::cout << 10 % 3;
}
1, 1

If either the divisor or dividend is a floating point number, regular division is performed instead:

#include <iostream>

int main() {
std::cout << 7.0 / 3;  // Outputs 2.33333

std::cout << ", ";

std::cout << 7 / 3.0;  // Also outputs 2.33333
}
2.33333, 2.33333
This Question is from the Lesson:

### Variables, Types and Operators

Learn the fundamentals of C++ programming: declaring variables, using built-in data types, and performing operations with operators

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

### Variables, Types and Operators

Learn the fundamentals of C++ programming: declaring variables, using built-in data types, and performing operations with operators

Part of the course:

## Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

### This course includes:

• 124 Lessons
• 550+ Code Samples
• 96% Positive Reviews
• Regularly Updated
• Help and FAQ
Free, Unlimited Access

### Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.