Java Program to Check Whether a Number is EvenorOdd

Check if a Number is Positive or Negative in Java

Given an integer input, The objective is to write a code to Check if a Number is Positive or Negative in Java Language.

For Instance,Input : Num = -5
Output : The number is Negative

Flow Diagram:-

flowchart-in-c21.png

Code:-

  • For a user input num

  • If the num > 0: it is a positive number.

  • If the num < 0: it is a positive number.

  • Else the number has to be zero itself

Algorithm:-

Code:-

class Main
{
  public static void main (String[]args)
  {

      Scanner sc=new Scanner(System.in);  

      System.out.print("Enter a number: ");    

      int num = sc.nextInt();  

    //Conditions to check if the number is negative or positive
    if (num > 0)
        System.out.println ("The number is positive");
    else if (num < 0)
        System.out.println ("The number is negative");
    else
        System.out.println ("Zero");
  }
}

Output:-

Enter a number: 22

The number is Positive