By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript Program > How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript
JavaScript Program

How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript

Rocoderes
Last updated: 2022/08/28 at 2:49 PM
Rocoderes
Share
7 Min Read
How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript

In this article, We will make some programs to perform basic mathematical operations like add two numbers, swap two numbers and find square root in our JavaScript. We will see these three programs one by one to understand from very basic.

Contents
Adding Two NumbersGetting Data From User and Perform AdditionSwapping Two NumbersFetching Values From User to SwapFinding The Square Root of The NumberFetching Value From User

These will be our basic programs, so that we can understand that how we can perform such kind of arithmetic operation in JavaScript. So without wasting more time, let’s jump into it.

Adding Two Numbers

So our first program will be to add two number, As we know that JavaScript cannot show it content without HTML. So we need to add HTML as well here, like we will just use it to show our output.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Addimg two numbers</title>
</head>
<body>
    <script>
        var a = 20;
        var b = 40;
        document.write("The addition between a and b will be"+ (a+b));

    </script>
    
</body>
</html>

Firstly, we have made an HTML file, in which we have just basic structure, we don’t need to touch that. We will just move to a body part, in this we have used the <script> tag to add the JavaScript code. In here, we have add variable a with 20 value, and variable b with 40 value. So, addition of these should be 60, we have here use document.write() method to show the output. We can also use alert() and, ⁣butconsole.log() these methods don’t show the output on the DOM or simply on screen, so document.write() is good here.

Note that: We are using here inner JavaScript code here, you can also write this code in the other file with .js extension and just add the path in the <script> tag’s src attribute.

How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript

Getting Data From User and Perform Addition

You can also add to numbers which are dynamic, or you can say data provided by user. For that, we just need to provide fields in which the user can enter the values. Here, we have used a prompt method in which we have added a message, Similarly we have added another prompt for b variable with the message. These prompt will pop up when the program will get loaded and simply show these messages. If users add some value, then that value will come as string. Now we have declared another variable named c, in this we have, firstly, converted the string value to integer using parseInt() the method. Then we have performed addition between these two.

<script>
        var a = prompt("Please enter first number ");
        var b = prompt("Please enter second number ");
        var c = parseInt(a) + parseInt(b);
        document.write("Addition will be "+c);

    </script>
How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript
How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript

Swapping Two Numbers

Next program will be swapping two numbers, Here we have again made the same HTML structure and added a <script> tag to add JavaScript. Here we have again added two variable a and b with some values. Then we printed the value of these, also we have used </br> which is actually a tag to provide new line to HTML DOM.

Now we have declared another variable named c. In this variable, we have assigned the value of a so c will 20, and a will have nothing in it. Then we have assigned the value of b to a, so a will be 40 and b will be empty. Lastly, we have assign to value of c to b, so b will be 20 and c will be empty. Hence, swap will be done, and just printed the result of the swapped value.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Addimg two numbers</title>
</head>
<body>
    <script>
        var a = 20;
        var b = 40;
        document.write("Before: a and b will be "+a+ " and " +b+ " </br>");
        var c;
        c=a;
        a=b;
        b=c;
        document.write("After: a and b will be "+a+" and "+b)

    </script>
    
</body>
</html>
How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript

Fetching Values From User to Swap

We will use similar method like using prompt to get values and using parseInt to convert String to Integer. Here you can see we have used prompt inside the parseInt, which can be also done in addition one. Here we will get integer directly in our variables.

<script>
        var a = parseInt(prompt("Please enter first value "));
        var b = parseInt(prompt("Please enter second value "));
        document.write("Before: a and b will be "+a+ " and " +b+ " </br>");
        var c;
        c=a;
        a=b;
        b=c;
        document.write("After: a and b will be "+a+" and "+b)

    </script>

Finding The Square Root of The Number

Now, lastly, we will find the square root of the number. First, we will again create the same HTML structure with <script> tag. To find out the square root of the number, we have created a variable named a with 64 value. Now we have added another variable named b in which we will catch the value of square root of a. In here, we will use the Math which is actually an object, and using this we can access math class’s methods and properties. We have used Math.sqrt(a) a method which will return the square root value of a.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Addimg two numbers</title>
</head>
<body>
    <script>
        var a = 64;
        var b = Math.sqrt(a);
        document.write("Square root of "+a+" will be "+b);

    </script>
    
</body>
</html>
How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript

Fetching Value From User

Here again we just use prompt and parseInt method to get the values as an integer, and then we just apply our previous formula to get the result.

<script>
        var a = parseInt(prompt("Please enter value "));
        var b = Math.sqrt(a);
        document.write("Square root of "+a+" will be "+b)
       

    </script>

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: programs
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Rocoderes
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article How to Print Hello World Using JavaScript with Three Diffrent Ways How to Print Hello World Using JavaScript with Three Different Ways
Next Article How to Convert Kilometers to Miles and Celsius to Fahrenheit How to Convert Kilometers to Miles and Celsius to Fahrenheit
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Computing Fibonacci Sequence in JavaScript

Computing Fibonacci Sequence in JavaScript

January 12, 2023
Swap Two Numbers Without A Third Variable

How to Swap Two Numbers Without A Third Variable in JS

December 12, 2022
Armstrong Number

How to Find an Armstrong Number Using JavaScript

September 23, 2022
Print The Table

How to Print The Table of Any User Defined Number Using Function

September 22, 2022
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?