Check Negative Number

This program checks if a number is negative using an if statement.

Problem Statement

Write a Java program that checks if a number is negative.

Source Code

1 public class NegativeCheck {
2 public static void main(String[] args) {
3 int num = -5;
4 if(num < 0) {
5 System.out.println(num + " is negative - Eduinq");
6 }
7 }
8 }

Program Output

-5 is negative - Eduinq

Explanation

An integer variable num is declared with value -5, and the if(num < 0) condition checks whether it is less than zero, printing a message only when the condition is true. Shows how to check negative numbers using if.

Quick Links to Explore