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);
}
}
Asked
Active
Viewed 45 times
-1

Ousmane D.
- 54,915
- 8
- 91
- 126

777
- 27
- 6
-
Can you explain your problem in a bit more detail? It's not clear what you actually want nor why the code shown doen't satisfy your needs. – fvu Oct 28 '17 at 12:07
-
You got 1 because `1 % 2 == 1`. What did you expect instead? – Kevin Anderson Oct 28 '17 at 12:09
-
I was expecting 0. How does it become 1 ? Please explain. – 777 Oct 28 '17 at 12:10
3 Answers
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

keivan shirkoubian
- 75
- 1
- 1
- 9
-
Okay. But remainder is 0 and zero is real. Why is 0 not the output? I am confused. and 0 is an integer too. – 777 Oct 28 '17 at 12:19
-
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