Fill an array (type int, size 10). Print the average of all the elements.
Advertisements
Question
Fill an array (type int, size 10).
Print the average of all the elements.
Code Writing
Advertisements
Solution
import java.util.*;
class clArrays
{
void main()
{
Scanner sc = new Scanner(System.in);
int R[] = new int[10];
System.out.println("Enter 10 elements for the array : ");
for(int j = 0; j <= 9; j++)
{
System.out.print("Fill cell number " + j + " : ");
R[j] = sc.nextInt();
}
//calculating sum and average
int sum = 0; double avg = 0.0;
for(int j = 0; j <= 9; j++)
{
sum = sum + R[j];
}
avg = (double) sum/10;
System.out.print(" Average of the array elements: " + avg);
}
}
Output:
Enter 10 elements for the array :
Fill cell num 0: 55
Fill cell num 1 : 66
Fill cell num 2 : 44
Fill cell num 3 : 33
Fill cell num 4 : 99
Fill cell num 5: 77
Fill cell num 6 : 88
Fill cell num 7: 11
Fill cell num 8 : 22
Fill cell num 9: 101
Average of the array elements : 59.0
shaalaa.com
Is there an error in this question or solution?
