🎓 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
Subtracting two numbers is a basic arithmetic operation commonly used in programming. In JavaScript, you can easily perform subtraction using the - operator. This guide will walk you through writing a JavaScript program to subtract two numbers.
Problem Statement
Create a JavaScript program that:
- Accepts two numbers.
- Subtracts the second number from the first number.
- Returns the result of the subtraction.
Example:
Input:
15and5Output:
10Input:
7and3Output:
4
Solution Steps
- Read the Input Numbers: Provide two numbers either as part of user input or directly in the code.
- Subtract the Numbers: Use the
-operator to subtract the second number from the first. - Display the Result: Print the result of the subtraction.
JavaScript Program
// JavaScript Program to Subtract Two Numbers
// Author: https://www.javaguides.net/
function subtractTwoNumbers(num1, num2) {
// Step 1: Subtract the second number from the first
const difference = num1 - num2;
// Step 2: Display the result
console.log(`The difference between ${num1} and ${num2} is: ${difference}`);
}
// Example input
let number1 = 15;
let number2 = 5;
subtractTwoNumbers(number1, number2);
Output Example
The difference between 15 and 5 is: 10
Example with Different Input
If you modify the input to:
let number1 = 7;
let number2 = 3;
The output will be:
The difference between 7 and 3 is: 4
Explanation
Step 1: Subtract the Two Numbers
- The numbers are passed as arguments to the
subtractTwoNumbers()function, where they are subtracted using the-operator.
Step 2: Display the Result
- The result of the subtraction is stored in the variable
differenceand displayed usingconsole.log().
Conclusion
This JavaScript program demonstrates how to subtract two numbers using the - operator. This basic arithmetic operation is essential in many programming tasks, and the program serves as a foundation for learning how to perform mathematical operations 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