Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate divide and modulo for integers in C#?

How can I calculate division and modulo for integer numbers in C#?

like image 373
kartal Avatar asked Mar 21 '11 20:03

kartal


People also ask

Can you use C modulo division operator?

Modulo Division operator % in C language can be used only with integer variables or constants.

How do you divide with modulo?

For p = prime , b^(-1) mod p = b^(p - 2) mod p . You don't need any modular inverses from this. Just simplify the fraction: N or N^2+5 will be divisible by 2 and 3 . So divide them and then you have (a*b) mod P .

How do you do modulo division in C?

We use the % to denote this type of operator (it's the percentile operator). The modulus operator is added in the arithmetic operators in C, and it works between two available operands. It divides the given numerator by the denominator to find a result.


2 Answers

Here's an answer from the MSDN documentation.

When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2. To determine the remainder of 7 / 3, use the remainder operator (%).

int a = 5; int b = 3;  int div = a / b; //quotient is 1 int mod = a % b; //remainder is 2 
like image 97
as-cii Avatar answered Sep 21 '22 20:09

as-cii


There is also Math.DivRem

quotient = Math.DivRem(dividend, divisor, out remainder); 
like image 45
danodonovan Avatar answered Sep 19 '22 20:09

danodonovan