Java Program to Check Number is Positive or Negative

Java Program to Check Number is Positive or Negative using if else

In java programming, if else statements can be used to do the different logic's based on condition.

This java program is used to check whether number is positive number or negative number using if else statements.

import java.util.Scanner;

public class Positive{
	
	public static boolean positive(int number){
		if(number >= 0){
			return true;
		}  else {
			return false;
		}
	}
	public static void main(String[] args){
		System.out.print("Enter Number: ");
		Scanner sc=new Scanner(System.in);
		int number = sc.nextInt();
		if(positive(number)){
			System.out.println("Number is positive!");
		} else {
			System.out.println("Number is negative!");
		}
	}
}
Output:
D:\Java_Programs>javac Positive.java
D:\Java_Programs>java Positive
Enter Number: 5
Number is positive!

D:\Java_Programs>javac Positive.java
D:\Java_Programs>java Positive
Enter Number: -54
Number is negative!

D:\Java_Programs>javac Positive.java
D:\Java_Programs>java Positive
Enter Number: 0
Number is positive!

Java Programming Checks Number is Positive or Negative using switch case and static method

This java program is used to identify positive or negative and uses different logic's for positive and negative using switch case statements.

Used indefinite loop to check the user input number is positive or negative, user has to specify n or no to exit the program.

import java.util.Scanner;

public class Positive{
	// Checks positive or negative
	public static int positive(int number){
		if(number > 0){
			return 1;
		} else if (number < 0){
			return 0;
		}
		else {
			return -1;
		}
	}
	public static void main(String[] args){

		Scanner sc=new Scanner(System.in);
		while(true){
			System.out.print("Enter Number: ");
			int number = sc.nextInt();
			int caseID = positive(number);
                        // different logics for positive, negative or neither positive nor negative.
			switch(caseID){
			case 0:
				System.out.println("Number is negative!");
				break;
			case 1:
				System.out.println("Number is positive!");
				break;
			default:
				System.out.println("Number is neither positve or positive!");
			}
			System.out.print("Do you want to check for other number (Y->yes / N ->No): ");
			String ch = sc.next();
			if(ch.toLowerCase() == "N" || ch.toLowerCase() == "no"){
				break;
			}		
		}
	}
}
Output:
D:\Java_Programs>javac Positive.java
D:\Java_Programs>java Positive
Enter Number: 5
Number is positive!
Do you want to check for other number (Y->yes / N ->No): y
Enter Number: 0
Number is neither positve or positive!
Do you want to check for other number (Y->yes / N ->No): Y
Enter Number: -6
Number is negative!
Do you want to check for other number (Y->yes / N ->No): no

Java Programming Checks Number is Positive or Negative using ternary operation

import java.util.Scanner;

public class Positive{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		while(true){
			System.out.print("Enter Number: ");
			int number = sc.nextInt();
			String msg = (number >= 0)?"Number is positive!":"Number is negative!";
			System.out.println(msg);
			System.out.print("Do you want to check for other number (Y->yes / N ->No): ");
			String ch = sc.next();
			if(ch.toLowerCase().equals("n") || ch.toLowerCase().equals("no")){
				break;
			}		
		}
	}
}
Output:
D:\Java_Programs>javac Positive.java
D:\Java_Programs>java Positive
Enter Number: 5
Number is positive!
Do you want to check for other number (Y->yes / N ->No): y
Enter Number: -8
Number is negative!
Do you want to check for other number (Y->yes / N ->No): n

Java Examples

GCD and Rational Number Java Program

Java Queue Program using exception

Java Stack Program using exception

Addition of three integers java program

Biggest of three integers java program

Fibonacci numbers java program

Arithmetic Operations Menu java program

Second Smallest Element In Array Java Program

Transpose Of A Matrix Java Program

Java Program to Display triangle of stars (*)

Java Programming Prints Product Tables

Java Program to Display triangle of numbers

Java Program to Get Current Date

Java Program to Find Character Vowel or Consonent

Java Program to Compute HCF and LCM

Java Program to Sum the Command Line Integer Arguments

Java Programm to Multiply the Command Line Integer Arguments

Java Program to Find Multiplication of Command Line Floating Point Numeric Arguments

Java Program to Check String Contains or Not

Java Program to Get Grade Description using Switch

Java Program for CSV File Reader

Java Program to Find Character Frequency Count in String using for each

Java Program to Find Min from Integer Array by Passing Array to Method

Java Program Linear Search

Binary Search Java Program

Ternary Search Java Program

Java Program to Generate Range of Numbers

Java Program to Generate Range of Characters

Java Program to Compute Square Root Value

Java Program to Check Number is Positive or Negative

Java Program to Check Number is Odd or Even

Java Program to Compute Plot Area

Java Program to Convert Number of Days into Years

Java Program to Check a Year is Leap Year, Century Year or Not

Java Program to Check a Character is Digit, Letter or neither digit nor letter

Java Program to Check a Number is Palindrome or not

Java Program to Sum Two Matrix

Java Program to Compute Power

Java Program to Check Number is an Armstrong Number or not

Java Program for Temperature Unit Conversions

Java Program to Generate Random Numbers in Specified Range

Java Program to Compute Sum of Digits and Product of Digits

Java Program to Compute Reverse Number

Java Programming Computes Factorial Value

Java Programming Checks Prime Number or not

Java Program to Compute Harmonic Series

Java Program Generate Floyd's Triangle

Java Program to Reverse String

Java Program to Check Palindrome String or not

Java Program to Open Notepad

Java Program to Search String using RegEx Pattern

Java Program to Search Word in String

Java Program to Get System Environment Variables

Java Program to Get IP Address of Server Name

Java Program for Arrays Sorting and Reverse using sort method

Java Program for Bubble Sorting

Java Program for Selection Sorting

Java Program for Insertion Sorting

Java Program for Merge Sorting

Java Program for Quick Sorting

Java Program for Counting Sort

Java Program for Radix Sorting

Java Program for Sorting Array of Strings

Java Program for String Characters Sorting

Java Program to Sum First N Numbers

Java Program to Product First N Numbers

Java Program to get URL details

Java Program to get URL HTML Content

Java Program to get URL details

Java Program to get URL HTML Content

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^