My program will not output the right answer if minus/negative doubles are inputted however the program will output the correct positive double. I think this is because I haven't used the Math.abs correctly but I don't know how to fix this.
My code is as follows:

public static void main(String[] args) {
        double cubed, squared, input1, input2, smallest;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter 2 numbers: ");
        input1 = scan.nextDouble();
        input2 = scan.nextDouble();

        cubed = Math.pow(input1, 3);
        input2 = Math.abs(input2);
        squared = Math.sqrt(input2);
        smallest = Math.min(squared, cubed);

        DecimalFormat fmt = new DecimalFormat("00.###");
        System.out.println(fmt.format(smallest));
 

    }

Recommended Answers

Can we see the input and output?

Jump to Post

All 3 Replies

Can we see the input and output?

Can we see the input and output?

Key:- Green = input

Input and Output Example (Correct):
Enter 2 numbers:
-68792.75 -459451.009
-40.975

Input and Output Actual (False):
Enter 2 numbers:
-68792.75 -459451.009
-325557730528518.94

Hey according to the programme that u have given the output is correct as Math.abs converts the negative value to a positive value and also the root that u will get as an output will be a positive value and if u cube a minus value u will get a minus value so the results are correct and the answer is also correct
Enter 2 numbers:
-68792.75 -459451.009
-325557730528518.94
Math.abs is used to get the absolute value of a number no matter it is positive or negative Math.abs(-5) gives 5
But if u r intending something different make it clear

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.