JavaScript Program to find Average of N Numbers

You are Here:

Using for loop

In the following example, we will find the average of numbers between 1 and 10 using for loop.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <h1>JS Average of N Numbers</h1> <script> var start = 1; var end = 10; var total = 0; var count = (end - start) + 1; var average = 0; document.write("Average of numbers between "+start+" to "+end+":<br>"); for(start = start; start <= end; start++) total += start; document.write("<br>Total = " +total); document.write("<br>Count = " +count); average = total / count; document.write("<br>Average = " +average); </script> </body> </html>

Using while loop

In the following example, we will find the average of numbers between 1 and 10 using while loop.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <h1>JS Average of N Numbers</h1> <script> var start = 1; var end = 10; var total = 0; var count = (end - start) + 1; var average = 0; document.write("Average of numbers between "+start+" to "+end+":<br>"); while(start <= end) { total += start; start++; } document.write("<br>Total = " +total); document.write("<br>Count = " +count); average = total / count; document.write("<br>Average = " +average); </script> </body> </html>

Using do while loop

In the following example, we will find the average of numbers between 1 and 10 using do while loop.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <h1>JS Average of N Numbers</h1> <script> var start = 1; var end = 10; var total = 0; var count = (end - start) + 1; var average = 0; document.write("Average of numbers between "+start+" to "+end+":<br>"); do { total += start; start++; }while(start <= end); document.write("<br>Total = " +total); document.write("<br>Count = " +count); average = total / count; document.write("<br>Average = " +average); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author