Java program to demonstrate the Math.copySign() method

Java example to demonstrate the Math.copySign() method.
Submitted by Nidhi, on May 16, 2022

Problem Solution:

In this program, we will use Math.copySign() method. The Math.copySign() method returns 1st argument with the sign of 2nd argument.

Program/Source Code:

The source code to demonstrate the Math.copySign() method is given below. The given program is compiled and executed successfully.

// Java program to demonstrate the 
// Math.copySign() method

import java.util.*;

public class Main {
  public static void main(String[] args) {
    double num = 0;

    num = Math.copySign(23.45, -45.67);

    System.out.print("Result: " + num);
  }
}

Output:

Result: -23.45

Explanation:

In the above program, we imported the "java.util.*" package to use the Scanner class. Here, we created a public class Main that contains a main() method.

The main() method is the entry point for the program. Here, we created a variable num of double type and Then we used Math.copySign() method. It returns 1st argument with the sign of 2nd argument and is assigned to the variable num. After that, we printed the value of the variable num.

Java Math Class Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.