🎓 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
Introduction
Finding the remainder when one number is divided by another is a common operation in programming known as the modulus operation. In JavaScript, this operation is performed using the % operator. This guide will walk you through writing a JavaScript program to find the remainder of two numbers.
Problem Statement
Create a JavaScript program that:
- Accepts two numbers.
- Finds the remainder when the first number is divided by the second number.
- Returns the result.
Example:
Input:
10and3Output:
1Input:
25and4Output:
1
Solution Steps
- Read the Input Numbers: Provide two numbers either by user input or directly in the code.
- Calculate the Remainder: Use the
%operator to find the remainder of the two numbers. - Handle Division by Zero: Ensure the divisor is not zero to avoid errors.
- Display the Result: Print the remainder.
JavaScript Program
// JavaScript Program to Find the Remainder of Two Numbers
// Author: https://www.javaguides.net/
function findRemainder(num1, num2) {
// Step 1: Handle division by zero
if (num2 === 0) {
console.log("Error: Division by zero is not allowed.");
return;
}
// Step 2: Calculate the remainder
const remainder = num1 % num2;
// Step 3: Display the result
console.log(`The remainder when ${num1} is divided by ${num2} is: ${remainder}`);
}
// Example input
let number1 = 10;
let number2 = 3;
findRemainder(number1, number2);
Output Example
The remainder when 10 is divided by 3 is: 1
Example with Different Input
If you modify the input to:
let number1 = 25;
let number2 = 4;
The output will be:
The remainder when 25 is divided by 4 is: 1
Division by Zero Example
let number1 = 10;
let number2 = 0;
The output will be:
Error: Division by zero is not allowed.
Explanation
Step 1: Handle Division by Zero
- Before performing the modulus operation, the function checks if the divisor (
num2) is zero. If it is, an error message is displayed to prevent a division by zero error.
Step 2: Calculate the Remainder
- The numbers
number1andnumber2are passed as arguments to thefindRemainder()function. The remainder is calculated using the%operator.
Step 3: Display the Result
- The remainder is stored in the variable
remainderand displayed usingconsole.log().
Conclusion
This JavaScript program demonstrates how to find the remainder of two numbers using the % operator. Handling division by zero ensures that the program runs without errors. The modulus operation is widely used in many programming scenarios, and this simple program provides a clear way to perform it in JavaScript.
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