× Introduction Algorithms Examples Flowcharts Coding

Algorithms Examples


Algorithm and JavaScript code to find area of a rectangle:


Algorithm:

Step1: Start

Step2: Take radius of a circle.

Step3: calculate: radius * radius * 3.14 and store it in area

Step4: Print area

Step5: Stop

JavaScript code:

let radius = parseInt(prompt('Enter a value for radius:'));

let area = radius * radius * 3.14;

alert("The area for the circle of radius: " + radius + " is " + area);

Algorithm and JavaScript code to find greater of two numbers:


Algorithm:

Step1: Start

Step2: Take value A and B

Step3: Check, if a>b go to step 4 else step 5

Step4: Print "A>B"

Step5: Print "B>A"

Step6: Stop

JavaScript code:

var a = parseInt(prompt("Enter value of A"));

var b = parseInt(prompt("Enter value of B"));

if(a > b){

alert("A is greater than B");

}else{

alert("B is greater than A");

}

Algorithm and JavaScript code to find area of a rectangle:


Algorithm:

Step1: Start

Step2: Take radius of a circle.

Step3: calculate: radius * radius * 3.14 and store it in area

Step4: Print area

Step5: Stop

JavaScript code:

let radius = parseInt(prompt('Enter a value for radius:'));

let area = radius * radius * 3.14;

alert("The area for the circle of radius: " + radius + " is " + area);

Algorithm and JavaScript code to print all natural numbers up to "100":


Algorithm:

Step1:Start

Step2:Store 1 in I

Step3:Check I value, if I <= 100 then go to step 4 else go to step 7

Step4:Print I

Step5:Increment I value by 1

Step6:Go to step 3

Step7: Stop

JavaScript code:

var i =1;

while (i <= 100){

alert(i);

i++;

}