Show In this tutorial, we will discuss the Java code to check if the given number is positive or negative or 0 In this post, we are going to learn how to check whether the given number is positive or Negative or zero using 6 ways The logic to check positive, negative or zero
Program to Check if the given number is positive or negative or 0Program to check if the given number is positive or negative or 0 – standard methodProgram 1 //Java program to check whether the given number positive or negative or zero class CheckPositiveNegative0{ public static void main(String args[]){ int num=456; if(num>0){ System.out.println(num+" is a positive number"); }else if(num<0){ System.out.println(num+" is a Negative number"); } else{ System.out.println("The given number is zero"); } } } When the above code is executed, it produces the following result 456 is a positive number In this program,
Program to check if the given number is positive or negative or 0 – Entered by userProgram 2 import java.util.Scanner; class CheckPositiveNegativezero{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the integer number: "); int num=scan.nextInt();//get input from the user for num if(num>0){ System.out.println(num+" is a positive number"); }else if(num<0){ System.out.println(num+" is a Negative number"); } else{ System.out.println("The given number is zero"); } } } When the above code is executed, it produces the following result Case 1 Enter the integer number:125 125 is a positive number Case 2 Enter the integer number:-453 -453 is a negative number Case 3 Enter the integer number:0 The given number is zero Approach
Program to check if the given number is positive or negative or 0 – using Nested ifProgram 3 import java.util.Scanner; class CheckPositiveNegativeNestedif{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the integer number: "); int num=scan.nextInt();//get input from the user for num if(num<=0){ if(num==0){ System.out.println("you entered zero"); }else{ System.out.println(num+" is a Negative number"); } } else{ System.out.println(num+" is a Positive number"); } } } When the above code is executed, it produces the following result Case 1 Enter the integer number:25 25 is a positive number Case 2 Enter the integer number:-43 -43 is a negative number Case 3 Enter the integer number:0 you entered zero Approach
Program to check if the given number is positive or negative or 0 – using methodProgram 4 import java.util.Scanner; class CheckPositiveNegativeMethod{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the integer number: "); int num=scan.nextInt();//get input from the user for num PosNegZero(num);//method call } public static void PosNegZero(int num){//method definition if(num>0){ System.out.println(num+" is a positive number"); }else if(num<0){ System.out.println(num+" is a Negative number"); } else{ System.out.println("The given number is zero"); } } } When the above code is executed, it produces the following result Case 1 Enter the integer number:234 234 is a positive number Case 2 Enter the integer number:-543 -543 is a negative number Case 3 Enter the integer number:0 The given number is zero Approach
Program to check if the given number is positive or negative or 0 – using ternary operatorProgram 5 import java.util.Scanner; class CheckPositiveNegativeternary{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the integer number: "); int num=scan.nextInt();//get input from the user for num1 PosNegZero(num);//method call } public static void PosNegZero(int num){//method definition System.out.println((num>=0)? "\n you have entered positive number" : "\n you have entered Negative number"); } } When the above code is executed, it produces the following result Case 1 Enter the integer number:657 you have entered positive number Case 2 Enter the integer number:-987 you have entered Negative number Approach
Program to check if the given number is positive or negative or 0 -using switch caseProgram 6 import java.util.Scanner; class CheckPositiveNegativeSwitch2{ public static int positive(int num){ if(num>0){ return 1; } else if(num<0){ return 0; } else{ return -1; } } public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("\nEnter the integer number: "); int num=scan.nextInt();//get input from the user for num int result=positive(num); switch(result){ case 0://check num is negative System.out.print(num+" is negative"); break; case 1://check num is positive System.out.print(num+" is positive"); break; default: System.out.print("the given number is zero"); break; } } } When the above code is executed, it produces the following result Case 1 Enter the integer number: 567 567 is positive Case 2 Enter the integer number: -876 -876 is negative Case 3 Enter the integer number: 0 The given number is zero Suggested post If statement in Java Nested if in Java Method in Java switch case in Java Operator in Java Data type and variables in Java Similar post C program to find if the given number is positive or negative or 0 C++ program to find if the given number is positive or negative or 0 Python program to find if the given number is positive or negative or 0 How do you know if a switch case is positive negative or zero?How to check positive negative or zero using switch case. (num > 0) return 1 ( true ) for positive number, otherwise 0 ( false ).. (num < 0) check negative and return 1 for negative number, otherwise 0.. (num == 0) return 1 for zero, otherwise 0.. How do you check whether a number is positive or negative or zero?Negative numbers are always written with a '−' sign in front of them and they are counted down from zero, i.e. -1, -2, -3, -4 etc. Always look at the sign in front of a number to check if it is positive or negative. Zero, 0, is neither positive nor negative.
How do you check an integer is positive or negative?If the Integer is greater than zero then it is a positive integer. If the number is less than zero then it is a negative integer. If the number is equal to zero then it is neither negative nor positive.
|