Answer to Question #233190 in Java | JSP | JSF for Srushti

Question #233190
code in java

Ram and Mohan are two brothers. They are not good at maths. So their father decided to give some assignments as a game so that they can enjoy as well as they can learn the concept of number systems. So he gave two numbers to them. One is the sum of two numbers, x and y, and another is the product of the same two numbers. Help them to write a code to find x and y.

Note:

The two numbers should be printed in ascending order.
Input

56

Output

23

Explanation

If we sum the numbers 2 and 3 we get the first input value 5 and if we multiply 2 and 3 we get the second input value 6. Hence the output is 2 and 3

Example 2

Input

15 50

Output

5 10

code in Java
1
Expert's answer
2021-09-04T10:55:45-0400
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("sum: ");
        int sum = in.nextInt();
        System.out.print("product: ");
        int product = in.nextInt();
        double sqrtD = Math.sqrt(Math.pow(-sum, 2) - (4 * product));
        double x1 = (sum + sqrtD) / 2;
        double x2 = (sum - sqrtD) / 2;
        System.out.println("x=" + x1 + " y=" + x2);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS