if I use double sum=0
instead of int sum=0
, I get the expected answer (38.8) but when I use int
I get 38.0 as answer. I want to know why is this happening since my return type is double
so I should not get automatically accurate decimal value output.
public class prg21 {
public static double average(int...ar)
{
int sum=0;
for(int x:ar)
{
sum+=x;
}
return sum/ar.length;
}
public static void main(String args[])
{
int []arr= {5,77,2,65,45};
System.out.println("Average="+average(arr));
}
}
I expect 38.8 but I get 38.0