🎓 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
Multiplying two numbers is a fundamental arithmetic operation and can be easily performed in JavaScript using the * operator. This guide will walk you through writing a JavaScript program to multiply two numbers.
Problem Statement
Create a JavaScript program that:
- Accepts two numbers.
- Multiplies the two numbers and returns the result.
Example:
Input:
5and3Output:
15Input:
7and4Output:
28
Solution Steps
- Read the Input Numbers: Provide two numbers either by user input or directly in the code.
- Multiply the Numbers: Use the
*operator to multiply the two numbers. - Display the Result: Print the result of the multiplication.
JavaScript Program
// JavaScript Program to Multiply Two Numbers
// Author: https://www.javaguides.net/
function multiplyTwoNumbers(num1, num2) {
// Step 1: Multiply the two numbers
const product = num1 * num2;
// Step 2: Display the result
console.log(`The product of ${num1} and ${num2} is: ${product}`);
}
// Example input
let number1 = 5;
let number2 = 3;
multiplyTwoNumbers(number1, number2);
Explanation
Step 1: Multiply the Two Numbers
- The numbers
number1andnumber2are passed as arguments to themultiplyTwoNumbers()function. Inside the function, the*operator is used to multiply these two numbers.
Step 2: Display the Result
- The result of the multiplication, stored in the variable
product, is displayed usingconsole.log().
Output Example
The product of 5 and 3 is: 15
Example with Different Input
If you modify the input to:
let number1 = 7;
let number2 = 4;
The output will be:
The product of 7 and 4 is: 28
Conclusion
This JavaScript program demonstrates how to multiply two numbers using the * operator. This simple arithmetic operation can be used in various programming tasks, and the program provides a straightforward way to perform multiplication in JavaScript. By following this method, you can easily extend the program to handle user input or more complex mathematical operations.
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