🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Determining if a number is positive or negative is a basic programming task that introduces conditional statements. This functionality can be useful in various applications, such as data validation or mathematical computations. In this blog post, we will write a simple Java program that checks if a given number is positive or negative.
2. Program Steps
1. Import the Scanner class to read user input.
2. Prompt the user to enter a number.
3. Use an if-else statement to check if the number is positive, negative, or zero.
4. Display the result.
5. Close the Scanner object to prevent resource leaks.
3. Code Program
import java.util.Scanner;
public class CheckNumberSign {
public static void main(String[] args) {
// Creating a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
double number = scanner.nextDouble(); // Reading the number
// Checking if the number is positive, negative, or zero
if (number > 0) {
System.out.println(number + " is a positive number.");
} else if (number < 0) {
System.out.println(number + " is a negative number.");
} else {
System.out.println("The number is zero.");
}
scanner.close(); // Closing the scanner
}
}
Output:
Enter a number: -5 -5 is a negative number.
Explanation:
1. The program begins by importing the Scanner class, which is used to obtain input from the user.
2. It then prompts the user to enter a number. This input is read and stored in a variable called number.
3. An if-else statement checks the value of number. If the value is greater than zero, it means the number is positive, and a corresponding message is displayed. If the value is less than zero, the number is negative, and a different message is shown. If the number is exactly zero, a specific message indicating that the number is zero is displayed.
4. This approach uses simple conditional logic to classify the number into positive, negative, or zero.
5. Finally, the Scanner object is closed to prevent resource leaks, which is a best practice for handling user input in Java.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment