not getting required return value [duplicate] - java

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
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

int datatype will drop any decimal value
you get 38.0 because the expression is saved into an int, dropping decimals and making it 38, then it's returned as a double so it's converted to 38.0

Related

JAVA multiplication of Double and Double gives additional information [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am using this JAVA program where I am multiplying a double with a double and storing it in a double.
My expected output is 7.14 as per my mathematics knowledge, but I get 7.140000000000001. I don't understand why is the 0000000000001 part is coming
public class Main
{
public static void main(String[] args) {
double a=7.0;
double b=1;
double c=a*b*1.02;
System.out.println(c);
}
}
So you are getting a super high level precision output for your operation. If you want to set a precision limit, you can do so following the example below
double no=12.786;
DecimalFormat dec = new DecimalFormat("#0.00");
System.out.println(dec.format(no));

Eclipse result integer value only [duplicate]

This question already has answers here:
Division operation is giving me the wrong result [duplicate]
(3 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Here is my code, Eclipse always return integer value of results, even if h is double. Please help me to fix this.
public static void main(String[] args) {
double h=0.0;
for(int i=1;i<=1000;i++) {
h=h+ 1/i;
}
System.out.println("Harmonic sum "+h);
System.out.println("Harmonic sum "+String.format("%.4f", h));
Result:
Harmonic sum 1.0
Harmonic sum 1,0000
You need to cast the result of your division into double
Try this out:
h=h+ (double)1/i;
I hope this is what you were looking for.

Addition of two double type variables returns very long decimal values [duplicate]

This question already has answers here:
Round a double to 2 decimal places [duplicate]
(13 answers)
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 5 years ago.
I have an application where I intend to add two double type values. In some certain situation, the result contains a very long decimal part. I was able to reproduce the problem in a separate program.
public class Test {
public static void main (String args []) {
double total1= 8.69;
double total2 = 1.69;
double totalamount = total1 + total2 ;
System.out.println(totalamount);
}
}
Output: 10.379999160766602 (The expexted output should be in the format of #.##)
I have tried changing the values of total1 and total2. The output is in expected format in some cases. The occurrence of the long values is very random and it goes with the float datatype as well.
What is the explanation for the above behavior?

Java - cast not producing expected output [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
public class Test {
public static void main(String[] args) {
int test = 1;
System.out.println((double)(Math.pow(test/++test, 2)));
}
0.0 is printed to the screen. Why? Why is the cast not working as expected?
If test is declared this way...
double test = 1;
I get what I expect to print... 0.25.
Why?
I am new to programming and I'm playing around. Reading some of the documentation at this level is next to useless.
You are doing integer division. SO change the line to
System.out.println((Math.pow(test/(double)++test, 2)));
to do double division

My java program show 0.0 in 15/100 [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 6 years ago.
Why does this program show 0.0?.
Below is my code:
class new1{
public static void main (String[]args){
int discount = 15;
float discount1 = 15/100;
System.out.println(discount1);
}
}
In line float discount1 = 15/100; is evaluated using the integer division.
If you want to get expected result, you should write like this
float discount1 = 15.0/100;

Categories

Resources