Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
JavaScript Program for Adding Two Numbers | 4 different ways

JavaScript Program for Adding Two Numbers | 4 different ways

Posted on October 19, 2022October 22, 2022

Table of Contents

  • JavaScript Program for Adding Two Numbers | 4 different ways
    • Code to Sum  of two numbers in JS
      •  Addition of two numbers in JS
      • Addition of two numbers in JS using HTML
      • Addition of two numbers in JS using function – #1
      • Addition of two numbers in JS using function – #2

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 onclick="add_Num()">Addition</button>
<p> sum =<input id="ans"></p>
</body>
</html>

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 onclick="add();">Add</button>
<button onclick="clr();">clear</button>
<p id="ans"></p>
</body>
</html>

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

After addition

JavaScript Program for Adding Two Numbers
Output 3

 

After clear

JavaScript Program for Adding Two Numbers
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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes