Publish Your Code, Article For Free​.

Table of Contents

Write a program in Java to check a given Number is positive, Negative or Zero

Deepak Kumar Mandal ( SDE at Cloudcraftz Solutions )

Deepak Kumar Mandal ( SDE at Cloudcraftz Solutions )

Table of Contents

The following concept will test whether a number is positive, negative or Zero by using Java Programming. It is done by checking where the number lies on the number line.

The following algorithm will help to check this condition.

  • If the input number is greater than zero then it is a positive number.
  • If the input number is less than zero it is a negative number.
  • If the number is zero then it is neither positive nor negative.

Flow Chart

Flow Chart

Working

  • Step 1. Start
  • Step 2. Enter the number
  • Step 3. If N > 0, N is postive.
  • Step 4. If N == 0 , N is Zero
  • Step 5. Otherwsie, N is Negative

Java Programme

import java.util.Scanner;

public class CheckNumber {  // Save your file name same as class name.

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter a number : ");
		int num = sc.nextInt(); // enter the value of num

		// coditions for checking entered number is positive, negative or zero.
		if (num > 0) {
			System.out.println("Entered number is positive");
		} else if (num == 0) {
			System.out.println("Entered number is zero");
		} else {
			System.out.println("Entered number is negative");
		}

	}

}

Output:
Enter a number:56                                                                                                                                                                                                         
Entered number is positive

 

Publish Your Code, Article For Free​.

you're currently offline