Java Program to Find the Sign of Given Number Using Library Method

Java program to find the sign of given number using library method

// Java program to find the sign of a given number 
// using the library method

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner X = new Scanner(System.in);

    double num = 0;

    System.out.print("Enter number: ");
    num = X.nextDouble();

    double res = Math.signum(num);
    if (res == 0)
      System.out.print("Number is zero.");
    else if (res == -1)
      System.out.print("Number is negative.");
    else if (res == 1)
      System.out.print("Number is positive.");
    else
      System.out.print("Number is NaN.");
  }
}

Output

Enter number: -34
Number is negative
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment