- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to find if the given number is positive or negative
Read a number from the user using the Scanner class's method. check whether the given number is greater, lesser or, equal to 0. If it is greater given number is positive if the lesser given number is negative. the else given number is neither positive or negative.
Example
import java.util.Scanner; public class PositiveOrNegative { public static void main(String args[]){ int num; System.out.println("Enter a number ::"); Scanner sc = new Scanner(System.in); num = sc.nextInt(); if (num > 0){ System.out.println("Given number is a positive integer"); } else if(num < 0){ System.out.println("Given number is a negative integer"); } else { System.out.println("Given number is neither positive nor negative integer"); } } }
Output 1
Enter a number :: 55 Given number is a positive integer
Output 2
Enter a number :: -88 Given number is a negative integer
Advertisements