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.###");

8 Years
Discussion Span
Last Post by Zaad

Can we see the input and output?

Key:- Green = input

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

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

Edited by anevins: n/a


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
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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.