Monday 9 February 2015

Filled Under:

How to find sum and average of array using loop

In this tutorial we try to illustrate how to find the sum and average of array to do this first we initialize an array of name marks having different values than we use loop for printing and adding elements of array here loop is start from 0 because array indexing always starts with 0 and in final step we calculate average by dividing total number of elements of an array.

CODING:

class mark
{
public static void main(String args[])
{
int t=0;
int marks[]={80,60,70,80,90,60,70,80};
for(int c=0;c<8;c++)
{
System.out.println("marks of "+(c+1)+" Subject="+marks[c]);
t=t+marks[c];
}
double avg=t/8;
System.out.println("Total marks ="+t);
System.out.println("Average ="+avg);
}
}


OUTPUT:




0 comments:

Post a Comment