🎓 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
Dividing two numbers is a basic arithmetic operation. In JavaScript, you can easily divide two numbers using the / operator. This guide will walk you through writing a JavaScript program to divide two numbers.
Problem Statement
Create a JavaScript program that:
- Accepts two numbers.
- Divides the first number by the second number.
- Returns the result.
Example:
Input:
10and2Output:
5Input:
15and3Output:
5
Solution Steps
- Read the Input Numbers: Provide two numbers either by user input or directly in the code.
- Divide the Numbers: Use the
/operator to divide the first number by the second. - Handle Division by Zero: Ensure that the divisor is not zero to avoid errors.
- Display the Result: Print the result of the division.
JavaScript Program
// JavaScript Program to Divide Two Numbers
// Author: https://www.javaguides.net/
function divideTwoNumbers(num1, num2) {
// Step 1: Handle division by zero
if (num2 === 0) {
console.log("Error: Division by zero is not allowed.");
return;
}
// Step 2: Divide the two numbers
const result = num1 / num2;
// Step 3: Display the result
console.log(`The result of dividing ${num1} by ${num2} is: ${result}`);
}
// Example input
let number1 = 10;
let number2 = 2;
divideTwoNumbers(number1, number2);
Explanation
Step 1: Handle Division by Zero
- Before performing the division, 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: Divide the Two Numbers
- The numbers
number1andnumber2are passed as arguments to thedivideTwoNumbers()function. The division is performed using the/operator.
Step 3: Display the Result
- The result of the division is stored in the variable
resultand displayed usingconsole.log().
Output Example
The result of dividing 10 by 2 is: 5
Example with Different Input
If you modify the input to:
let number1 = 15;
let number2 = 3;
The output will be:
The result of dividing 15 by 3 is: 5
Division by Zero Example
let number1 = 10;
let number2 = 0;
The output will be:
Error: Division by zero is not allowed.
Conclusion
This JavaScript program demonstrates how to divide two numbers using the / operator while ensuring that the divisor is not zero. Handling division by zero is important to avoid runtime errors. This simple arithmetic operation is widely used in various programming tasks, and the program provides a straightforward solution for performing division 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