Write a program to sum and multiply two numbers using javascript
Answers
<html>
<script>
function add(){
var num1=document.form.text1.value;
var num2=document.form.text2.value;
var sum=Number(num1)+Number(num2);
alert("Sum of two numbers is: "+sum);
}
function divide(){
var num1=document.form.text1.value;
var num2=document.form.text2.value;
var sum=Number(num1)/Number(num2);
alert("Result is: "+sum);
}
function product(){
var num1=document.form.text1.value;
var num2=document.form.text2.value;
var sum=Number(num1)*Number(num2);
alert("Product is: "+sum);
}
function modulus(){
var num1=document.form.text1.value;
var num2=document.form.text2.value;
var sum=Number(num1)%Number(num2);
alert("Modulus is: "+sum);
}
</script>
<form name="form">
<table>
<tr>
<td>Enter First Number:</td>
<td><input type="text" name="text1"></td>
</tr>
<tr>
<td>Enter Second Number:</td>
<td><input type="text" name="text2"></td>
</tr>
<tr>
<td><input type="button" value="Add" onclick="add();"></td>
<td><input type="button" value="Divide" onclick="divide();"></td>
</tr>
<tr>
<td><input type="button" value="Product" onclick="product();"></td>
<td><input type="button" value="Modulus" onclick="modulus();"></td>
</tr>
</table>
</html>
→ Program to sum and multiply two numbers using javascript:
// Declare two variables num1 and num2 to store the given numbers:
let num1 = 10;
let num2 = 20;
// Calculate the sum of the two given numbers:
let sum = num1 + num2;
// Calculate the product of the two given numbers:
let product = num1 * num2;
// Print the resultant sum and product to the console:
console.log("Sum: " + sum);
console.log("Product: " + product);
→ Explanation:
(1.) We will declare two variables num1 and num2 and assign them some arbitrary values.
(2.) The sum of the two given variables is calculated and then stored in another variable sum. The addition operation is performed using the + operator.
(3.) The product of the two given variables is calculated and then stored in another variable product. The multiplication operation is performed using the * operator.
(4.) Finally, the resultant variables of the sum and product are printed to the console using the console.log() method.
#SPJ3