Arithmetic Caculation

JavaScript Program for Adding Two Numbers | 4 different ways

JavaScript Program for Adding Two Numbers | 4 different ways

In this article, we will discuss the concept of JavaScript Program for Adding Two Numbers

In this post, we are going to learn how to write a program to find  addition of two numbers and dispay result in JS language

Code to Sum  of two numbers in JS

Addition of two numbers in JS

In this code, we are going to learn how to write a program to addition of two numbers in JS programming language

In this program, the user initiates the values to variables and calculates the sum of the given numbers using plus(+) operator

Program 1

// JavaScript proram to addition of two numbers

const f_Num=17;
const l_Num=13;
//declare and initaiize two variable

//calculate sum of two numbers using '+' operator
const sum=f_Num+l_Num;

//display the sum of two numbers
console.log('The addition of '+f_Num+' and '+l_Num+' is: '+sum);

When the above code is executed, it produces the following result

The addition of 17 and 13 is: 30

 

Addition of two numbers in JS using HTML

In this program, the user initiates the values to variables and calculates the sum of the given numbers using plus(+) operator inside the HTML tag

Program 2

<html>
<head>
<title>Sum of two numbers</title>
<script>
var f_Num=10;
var l_Num=20;
var add=f_Num+l_Num;
document.write("The sum of "+f_Num+" and "+l_Num+" is: "+add);
</script>
</head>
<body>

</body>
</html>

When the above code is executed, it produces the following result

The Sum of 10 and 20 is: 30

 

Addition of two numbers in JS using function – #1

In this program, the program is asked to enter two numbers from the user and calculates the sum of the given numbers using the function in JavaScript language

Program 3

<html>
<head>
<title>Sum of two numbers</title>
<script>
function add_Num()//define function for addition
{
var fNum,sNum,sum;
f_Num=parseInt(document.getElementById("first").value);
s_Num=parseInt(document.getElementById("second").value);
sum=f_Num+s_Num;
document.getElementById("ans").value=sum;
}
</script>
</head>
<body>
<p>Enter the first value:<input id="first"></p>
<p>Enter the second value:<input id="second"></p>
<button >

When the above code is executed, it produces the following result

Addition of two numbers in JS using function – #2

In this program, the program is asked to enter two numbers from the user and calculates the sum of the given numbers using the function in JavaScript language

Program 4

<html>
<head>
<title>Add two numbers</title>
<script>
function add()//function to addition
{
var f_Num,s_Num,sum;
f_Num=Number(document.getElementById("first").value);
s_Num=Number(document.getElementById("second").value);
sum=f_Num+s_Num;
document.getElementById("ans").innerHTML="Total:"+sum;
}
function clr()//function to clear the fields
{
document.getElementById("first").value="";
document.getElementById("second").value="";
document.getElementById("ans").innerHTML="";
}
</script>
</head>
<body>
<p>Enter the first value:<input type="text" id="first"></p>
<p>Enter the second value:<input type="text" id="second"></p>
<button >

When the above code is executed, it produces the following result

After addition

Output 3

 

After clear

Output 5

Suggested for you

Java program to add two numbers

C program to add two numbers

C++ program to add two numbers

Python program to add two numbers

 

Write a program to sum of prime numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

 

Write a program to sum of natural numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.