N
N
Nik_Haker2015-01-23 20:33:30
Programming
Nik_Haker, 2015-01-23 20:33:30

How to determine if there is a remainder of a division in c++?

for example, we divide 5 by 2. there is a condition if (there is a remainder) ... then we do something. And the integer part of the division goes into another variable: variable=integer part(5/2)=2
how to do it?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Armenian Radio, 2015-01-23
@gbg

const int a=5%2; //% - вычислить остаток от деления левого на правое
cout<<a;

---------------------------------
1

I
Igor Ermolaev, 2015-01-23
@ErmIg

In C++, the remainder of a division is determined by the % operator. For example:
int a = 5 % 3;

J
jackroll, 2015-01-23
@jackroll

Nik_Halker, read https://vk.com/doc268862046_361408455 and watch https://www.youtube.com/user/KennyMcKormic123/videos

H
hiloader, 2015-01-24
@hiloader

There is also a function that will return both the integer part and the remainder of the division. If both components are required, then it is better to use it.

#include <cstdlib>

div_t res = div(5,3);
// res.quot 1 -- целая часть
// res.rem 2 -- остаток

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question