Find sum of elements of array

You are given an array of integers of size N *N. Find the sum of elements in array?
Accept the input from console.
This is the simple array program and asked in entry level college exams.

The first line of input consists of an integer NN. The next line contains N * N space-separated integers representing the array elements.

111 3 7 23 98 56


Java program:

import java.util.Scanner;

public class ArraySum {

	private static long calculateArraySum(int[] arr) {
		long sum = 0;
		for (int i = 0; i < arr.length; i++) {
			sum += arr[i];
		return sum;

	public static void main(String[] args) {
		Scanner in = new Scanner(;
		int n = in.nextInt();

		int arr[] = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = in.nextInt();


I hope you liked the solution.
Please comment for questions or doubts.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.