Write a program to check number is positive or negative in java


In this core java programming tutorial we will Write a program to check number is positive or negative in java.



Contents of page >
  • Full Program/SourceCode / Example 1 to find number is positive or negative in java>
  • Full Program/SourceCode / Example 2 to find number is positive or negative Using java.lang.Math Class in java



You must know >
Any number above zero is positive.
Any number below zero is negative.
Zero is neither positive nor negative.



Full Program/SourceCode / Example 1 to find number is positive or negative in java>
/*
* Write a program to check number is positive or negative in java
*/
public class CheckNumberIsPositiveOrNegativeInJava {
   public static void main(String[] args) {
          int firstNumber = 7;
          checkNumberIsPositiveOrNegative(firstNumber);
         
          int secondNumber = -6;
          checkNumberIsPositiveOrNegative(secondNumber);
          int thirdNumber = 0;
          checkNumberIsPositiveOrNegative(thirdNumber);
   }
  
   /**
   * Method to check whether number is Positive Or Negative in java
   */
   static void checkNumberIsPositiveOrNegative(int number){
          if(number>=0)
                 System.out.println(number + " is positive");
          else if(number<=0)
                 System.out.println(number + " is negative");
          else if(number==0)
                 System.out.println(number + " is neither positive nor negative");
   }
  
}
/*OUTPUT
7 is positive
-6 is negative
0 is neither positive nor negative
*/




Full Program/SourceCode / Example 2 to find number is positive or negative Using java.lang.Math Class in java >
We will be using Math.signum(number) method
Which returns 1.0 if number is positive
Which returns -1.0 if number is negative
Which returns 0.0 if number is zero
/*
* Write a program to check number is positive or negative in java Using java.lang.Math Class
*/
public class CheckNumberIsPositiveOrNegativeUsingMathClass {
   public static void main(String[] args) {
          int firstNumber = 7;
          checkNumberIsPositiveOrNegative(firstNumber);
         
          int secondNumber = -6;
          checkNumberIsPositiveOrNegative(secondNumber);
          int thirdNumber = 0;
          checkNumberIsPositiveOrNegative(thirdNumber);
   }
  
   /**
   * Method to check whether number is Positive Or Negative in java
   */
   static void checkNumberIsPositiveOrNegative(int number){
         
          if(Math.signum(number) == 1.0d)
                 System.out.println(number + " is positive");
          else if(Math.signum(number) == -1.0d)
                 System.out.println(number + " is negative");
          else if(Math.signum(number) == 0.0d){
                 System.out.println(number + " is neither positive nor negative");
          }
   }
  
}
/*OUTPUT
7 is positive
-6 is negative
0 is neither positive nor negative
*/


So, in this core java programming tutorial we wrote a Wrote a program to check number is positive or negative in java in 2 ways.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Write a program to add two numbers in java

Write a program to Subtract two numbers in java

Multiply two numbers in java

program to Divide two numbers in java


No comments:

Post a Comment