Java Program To Find The Sum of Elements In An Array

Problem Statement :

 Given an array of integers, find the sum of its elements.

Function Description

 It must return the sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

  • ar: an array of integers

Input Format

The first line contains an integer, n, denoting the size of the array.
The second line contains, n, space-separated integers representing the array's elements.

Output Format

Print the sum of the array's elements as a single integer.

Sample Input

6
1 2 3 4 10 11

Sample Output

31
Program:
public class Sum{
public static void main(String[] args) {
         int [] arr = {1,2,3,4,10,11,749};
         int length=arr.length;
         int sum=0;
         for(int i=0;i<length;i++){
             sum += arr[i];
         }
          System.out.println(sum);

    }
}

Comments

Popular posts from this blog

First Program

Java program to find Plus Minus