Question
Print the average of all elements of an array.
Input:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10
ARRAY AFTER DELETING EVEN POSITION ELEMENTS:
2 4 6 8 10
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class averageOfElementInAnArray
{
public static void main(String[] args)
{
/* Initialize array */
double arr[];
double sum=0;
int size=0,i=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
size = sc.nextInt();
System.out.println("Enter numbers in array");
arr=new double[size];
for(i=0;i< size;i++)
{
arr[i]=sc.nextDouble();
}
System.out.println("Element entered in given array");
for(i=0;i< size;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
for ( i = 0; i < size; i++)
{
sum=sum+arr[i];
}
double average=sum/size;
System.out.println("Average of all element in the array is "+average);
}
}
Python
size=int(input("Enter the size of List:"))
li=[]
print("Enter Elements in list:")
for i in range(0,size):
print((i+1),end=":")
li.append(float(input()))
#Printing Elements in given List
print("Elements in given List:")
print(li)
totalSum=0
#Finding the Average of given array
for element in li:
totalSum=totalSum+element
averageOfGivenList=totalSum / size
print("Average of Given list:",averageOfGivenList)