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:
5
and3
Output:
15
Input:
7
and4
Output:
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
number1
andnumber2
are 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.
Comments
Post a Comment
Leave Comment