22) Write a JavaScript code to calculate maximum, minimum, sum and average of numbers in an array. Make all validation.


<HTML>

<HEAD>
<TITLE> Max Min Sum Avg</TITLE>
</HEAD>

<BODY>
<H1> Maximum Minimum Sum Avg</H1>
<p>
<SCRIPT>
var i,n;
var max=0;
var min=0;
var sum=0;
var avg=0;

n=parseInt(prompt(“Enter array size”));
var a=new Array(n);

for(i=0;i<n;i++)
{
a[i]=parseInt(prompt(“Enter numbers”));
}

// finding Maximum……..
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}

// finding Minimum……..
min=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
min=a[i];
}

// finding Sum of Array Numbers……..
sum=0;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}

// finding Average……..

avg=sum/n;

document.write(“Array Elements : – “);
for(i=0;i<n;i++)
{
document.write(a[i]+” “);
}

document.write(“<br>”+”Maximum Number= “+max+”<br>”);
document.write(“Minimum Number= “+min+”<br>”);
document.write(“Sum of Array Numbers= “+sum+”<br>”);
document.write(“Average= “+avg+”<br>”);
</SCRIPT>
</BODY>
<HTML>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s