Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get integer quotient when divide two values in c#?

Tags:

c#

vb.net

I want get integer quotient when I divide two values. Per example

X=3
Y=2
Q=X/Y = 1.5 // I want get 1 from results


X=7
Y=2
Q=X/Y=3.5 //I want get only 3 from results
like image 616
James123 Avatar asked Jul 26 '10 17:07

James123


People also ask

How do you find the quotient of two numbers in C?

printf("Enter dividend: "); scanf("%d", &dividend); printf("Enter divisor: "); scanf("%d", &divisor); Then the quotient is evaluated using / (the division operator), and stored in quotient . quotient = dividend / divisor; Similarly, the remainder is evaluated using % (the modulo operator) and stored in remainder .

How do you find the integer quotient?

The quotient is obtained after the process of division is completed. This means when a divisor divides a dividend, the answer that we get is the quotient. In other words, the quotient can be found using the formula, Dividend ÷ Divisor = Quotient. Let us understand this by a simple example of 12 ÷ 4 = 3.

Which operator gives integer quotient after division?

The following example uses the \ operator to perform integer division. The result is an integer that represents the integer quotient of the two operands, with the remainder discarded.

Is division in C integer division?

Integer division yields an integer result. For example, the expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3. C provides the remainder operator, %, which yields the remainder after integer division. The remainder operator is an integer operator that can be used only with integer operands.


1 Answers

Integer math is going to do this for you.

int x = 3 / 2; // x will be 1
int y = 7 / 2; // y will be 3
int z = 7 % 2; // z will be 1

If you were using decimal or floating-point values in your equations, that would be different. The simplest answer is to cast the result to an int, but there are static Math functions you could also use.

double a = 11d;
double b = 2d;
int c = (int)(a / b); // showing explicit cast, c will be 5
like image 133
Anthony Pegram Avatar answered Sep 22 '22 07:09

Anthony Pegram