हिंदी

Write a program for the following. Create an array DW[ ] of type double and size 8 and fill it with variables. Calculate the average of the maximum and the minimum element. For example: Input 30.8

Advertisements
Advertisements

प्रश्न

Write a program for the following.

Create an array DW[ ] of type double and size 8 and fill it with variables. Calculate the average of the maximum and the minimum element.

For example:

Input 30.8 27.5 18.2 35.2 21.1 25.6 8.8 9.4
Output Max = 35.2 Min = 8.8 Average = 22.0  
कोड लेखन
Advertisements

उत्तर

import java.util.Scanner;
public class CalculateAverage 
{
    public static void main(String[] args) 
    {
        double DW[] = new double[8];
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter 8 double elements:");
        for (int j = 0; j < DW.length; j++) 
        {
            DW[j] = sc.nextDouble();
        }
        double max = DW[0];
        double min = DW[0];
        
        for (int j = 1; j < DW.length; j++) 
        {
            if (DW[j] > max) 
            {
                max = DW[j];
            }
            if (DW[j] < min) 
            {
                min = DW[j];
            }
        }
        
        double average = (max + min)/2.0;
        System.out.println("Max =" + max);
        System.out.println("Min =" + min + "\tAverage =" + average);
                sc.close();
    }
}
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 7: Arrays - One Dimension - Exercises [पृष्ठ १७६]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 7 Arrays - One Dimension
Exercises | Q 4. (ii) | पृष्ठ १७६
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×