Define a class to declare an array of size 20 of the double datatype, accept the elements into the array and perform the following: Calculate and print the sum of all the elements.
Advertisements
प्रश्न
Define a class to declare an array of size 20 of the double datatype, accept the elements into the array and perform the following:
- Calculate and print the sum of all the elements.
- Calculate and print the highest value of the array.
संक्षेप में उत्तर
Advertisements
उत्तर
import java.util.Scanner;
public class MaxSum
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
double arr[] = new double[20];
System.out.println("Enter 20 numbers:");
for (int i = 0; i < 20; i++)
{
arr[i] = in.nextDouble();
}
int max = arr[0], sum = 0;
for (inti= 0; i <arr.length; i++)
{
if (arr[i] >max)
max = arr[i];
sum += arr[i];
}
System.out.println("Largest Number = " +max);
System.out.println("Sum = " +sum);
}
}shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
