I wrote a program to calculate the multiplication of odd numbers between 1
and 100
. So why does changing the data type give me a whole different output? Why is the output a negative number when I use int
? Additionally, the other results seem weird.
Code:
long total = 1L;
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
total *= i;
}
}
System.out.println(total);
The output in different cases :
5196472710489536419 (if total is long
)
-373459037 (if total is int
)
2.7253921397507295E78 (if total is double
)
Infinity (if total is float
)