Computation with the elements of an array | AskTheCode

Java program to compute for sum, average, highest & lowest in a user given array |

Problem:

Write a program to implement the following element to compute for sum, average and to find the highest and lowest score.


Sample input:

Enter the size of the array:

6

Enter the elements:

75 85 95 90 100 80


Sample output:

The elements in the array:

75 85 95 90 100 80

Sum of the elements: 525

Average of the elements: 87

Highest element: 100

Lowest element: 75

Code:

import java.util.Scanner;
import java.util.Arrays;

public class Computation_in_Array{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the size of the array: ");
		int n = sc.nextInt();
		int[] array = new int[n];

		System.out.println("Enter the elements: ");
		for (int i = 0; i < n; i++)
			array[i] = sc.nextInt();
		System.out.println("The elements in the array: ");
		for (int i : array)
			System.out.print(i+" ");
		int sum_res = sum(array);
		System.out.println("\nSum of the elements: "+sum_res);
		System.out.println("Average of the elements: "+sum_res/n);
		Arrays.sort(array);
		System.out.println("Highest element: "+array[n-1]+"\nLowest element: "+array[0]);
	}
	static int sum(int[] array){
		int sum = 0;
		for (int i : array) {
			sum += i;
		}
		return sum;
	}
}
10 views0 comments

Recent Posts

See All

Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes