-1
package PracticePackage;

public class whileLoop {

    public static void main(String[] args) {

        int i=1;

        System.out.println("Quotient "+i/2);    
        System.out.println("Remainder "+i%2);

    }

}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
777
  • 27
  • 6

3 Answers3

0

because integers are not real numbers so you get 1 as answer and the real part of remainder is ignored since you defined i as an integer

lrnzcig
  • 3,868
  • 4
  • 36
  • 50
0

this is the fomula that Java uses to yield the remainder of its operands:

(a/b)*b+(a%b)

where a is the dividend and b is the divisor.

so in your case it's like:

int i = 1; 
int b = 2;
int result = (i / b) * b + (i % b);

hence the result 1 rather than 0

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

1/2 = 0.5

you defined i as int

Integral division in java takes floor of the answer if the answer is a real number so 1/2 becomes 0, making 1%2 equal to 1

I hope that explains.

hsnsd
  • 1,728
  • 12
  • 30