Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Program to find if the given number is positive or negative or 0 in C

Java code to check if the given number is positive or negative or 0

Posted on October 8, 2020October 8, 2020

Table of Contents

  • Java code to check if the given number is positive or negative or 0
    • Program to Check if the given number is positive or negative or 0
      • Program to check if the given number is positive or negative or 0 – standard method
      • Program to check if the given number is positive or negative or 0 – Entered by user
      • Program to check if the given number is positive or negative or 0 – using Nested if
      • Program to check if the given number is positive or negative or 0 – using method
      • Program to check if the given number is positive or negative or 0 – using ternary operator
      • Program to check if the given number is positive or negative or 0 -using switch case

Java code to check if the given number is positive or negative or 0

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

  • Check the condition if(num<0), then number is negative
  • Check the condition if(num>0), then number is positive
  • Check the condition if(num==0), then number is zero

Program to Check if the given number is positive or negative or 0

Program to check if the given number is positive or negative or 0 – standard method

Program 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,

  • integer variable num  is declared and initialized
  • The given numbers are tested whether it is positive, negative or zero using if- else statements
  • Then,the program is displayed the output using System.out.print() function.

Program to check if the given number is positive or negative or 0 – Entered by user

Program 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

  • integer variable num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • The given number is tested whether it is positive, negative or zero using if- else statements
  • Then,the program is displayed the output of using System.out.print() function,

 

Program to check if the given number is positive or negative or 0 – using Nested if

Program 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

  • integer variable num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • The given number is tested whether it is positive, negative or zero using Nested if- else statements
  • Then,the program is displayed the output of using System.out.print() function,

 

Program to check if the given number is positive or negative or 0 – using method

Program 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

  • integer variable num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • The given number is tested whether it is positive, negative or zero using user defined method
  • Then,the program is displayed the output of using System.out.print() function,

 

Program to check if the given number is positive or negative or 0 – using ternary operator

Program 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

  • integer variable num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • The given number is tested whether it is positive, negative or zero using ternary operator
  • Then,the program is displayed the output of using System.out.print() function,

 

Program to check if the given number is positive or negative or 0 -using switch case

Program 6

import java.util.Scanner;
class CheckPositiveNegativeSwitch1{
  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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes