English

Fill an array (type int, size 10). Print the average of all the elements.

Advertisements
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?
Chapter 11: Programs Overall - Arrays [Page 252]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 11 Programs Overall
Arrays | Q 2. | Page 252
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×