In this tutorial, we will discuss a concept in Java program to check whether a number is Positive or Negative or Zero Show In this post, we will learn how to check if the number is positive or negative or zero in Java programming language Logic of the program If the number is greater than zero it is a positive number – (if the number>=0) If the number is less than to zero it is a Negative number – (if the number<=0) if the number is equal to zero the number is zero – (if the number==0) Java program to number is Positive or Negative or ZeroUsing if else if statementsProgram 1 This program allows the user to enter a number and check if the number is positive or negative or zero using if else if statements import java.util.Scanner; class CheckPosNegZero{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number: "); int num1=scan.nextInt();//get input from the user for num1 if(num1>0){ System.out.println(num1+" is a positive number"); } else if(num1<0){ System.out.println(num1+" is a Negative number"); } else{ System.out.println("number is Zero"); } } }When the above code is executed, it produces the following result case 1 Enter the number: 4 4 is a positive numbercase 2 case 3 Enter the number: 0 Number is zeroUsing nested if statementsProgram 2 This program allows the user to enter a number and check if the number is positive or negative or zero using nested if statements import java.util.Scanner; class CheckPosNegZero1{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number: "); int num1=scan.nextInt();//get input from the user for num1 if(num1<=0){ if(num1==0){ System.out.println(" you entered zero"); } else{ System.out.println(num1+" is a Negative number"); } } else{ System.out.println(num1+" is a positive number"); } } }When the above code is executed, it produces the following result case 1 Enter the number: 20 20 is a positive numbercase 2 Enter the number: -15 -15 is a Negative numbercase 3 Enter the number: 0 you entered zeroSimilar post C program to check whether a number is Positive or Negative or Zero C++ program to check whether a number is Positive or Negative or Zero Python program to check whether a number is Positive or Negative or Zero Suggested for you Operator in java Data type and variable in Java If statement in Java Nested if statements in Java In this post, we will write two java programs, first java programs checks whether the specified number is positive or negative. The second program takes the input number (entered by user) and checks whether it is positive or negative and displays the result. → If a number is greater than zero then it is a positive number → If a number is less than zero then it is a negative number → If a number is equal to zero then it is neither negative nor positive. Write a Java program that reads an integer and check whether it is negative, zero, or positive. Test Data Pictorial Presentation: Sample Solution: Java Code: import java.util.Scanner; public class Exercise27 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input a number: "); int n = in.nextInt(); if (n > 0) { System.out.println("Number is positive"); } else if (n < 0) { System.out.println("Number is negative"); } else { System.out.println("Number is zero"); } } }Sample Output: Input a number: 7 Number is positiveFlowchart: Java Code Editor: Contribute your code and comments through Disqus. Previous: Write a Java program to display the following character rhombus structure.
What is the difficulty level of this exercise?
Syntax for creating a two-dimensional array in Java Try the following : int[][] multi = new int[5][10];... which is a short hand for something like this: int[][] multi = new int[5][]; multi[0] = new int[10]; multi[1] = new int[10]; multi[2] = new int[10]; multi[3] = new int[10]; multi[4] = new int[10];Note that every element will be initialized to the default value for int, 0, so the above are also equivalent to: int[][] multi = new int[][]{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };Ref: https://bit.ly/3vYSxFz
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 methodThis 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): noJava Programming Checks Number is Positive or Negative using ternary operationimport 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 Programming Language Blog |