🎓 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
The modulo (or remainder) operator, denoted as % in Java, is a fundamental arithmetic operator that finds the remainder of a division operation between two numbers. Unlike the division operator that returns the quotient, the modulo operator returns the remainder when the first operand is divided by the second. This operator is widely used in programming for tasks such as determining even or odd numbers, implementing cyclic operations, and more.
2. Program Steps
1. Declare and initialize two integer variables.
2. Use the modulo operator to find the remainder of their division.
3. Demonstrate using the modulo operator with positive and negative operands.
4. Print the results to the console.
3. Code Program
public class ModuloExample {
public static void main(String[] args) {
// Step 1: Declaring and initializing two integer variables
int dividend = 25;
int divisor = 4;
// Step 2: Using the modulo operator to find the remainder
int remainder = dividend % divisor;
// Step 3: Demonstrating the use of the modulo operator
System.out.println("The remainder of " + dividend + " divided by " + divisor + " is " + remainder);
// Additional demonstration with negative operands
int negativeDividend = -25;
int remainderWithNegative = negativeDividend % divisor;
System.out.println("The remainder of " + negativeDividend + " divided by " + divisor + " is " + remainderWithNegative);
}
}
Output:
The remainder of 25 divided by 4 is 1 The remainder of -25 divided by 4 is -1
Explanation:
1. The program starts by defining two integers, dividend (25) and divisor (4), to be used in the modulo operation.
2. It then calculates the remainder of dividing dividend by divisor using the modulo operator % and stores the result in the integer variable remainder.
3. The program prints the result, showing that the remainder of dividing 25 by 4 is 1. This demonstrates a basic use of the modulo operator.
4. To further explore the operator's behavior, the program also calculates the remainder using a negative dividend (-25) and prints the result. This demonstrates that when the dividend is negative, the remainder follows the sign of the dividend.
5. This example illustrates how the modulo operator works in Java, including its use with both positive and negative operands. The modulo operator is useful in various programming scenarios, including mathematical calculations, loop control, and conditional logic.
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