# Program to find the sum of elements in a given array | faceprep

Program to find the sum of elements in a given array is discussed here. Given the number of elements of the array and the array elements, the sum of the elements of the array is produced as output.

##### Input Format:
• Input the number of elements of the array
• Input the Array elements

3
1
2
3

## Algorithm to find the sum of elements in a given array

• Input the number of elements of the array.
• Input the array elements.
• Sum = addition of all the array elements.
• Display sum.

Program to find the sum of elements in a given array is given below.

// C program to find the sum of elements in a given array
#include<stdio.h>
int main()
{
//fill the code;
int n;
printf(“Enter the size of array:”);
scanf(“%d”,&n);
int arr[n];
int i, sum = 0;
printf(“Enter the elements of array:”);
for(i = 0; i < n; i++)
{
scanf(“%d”,&arr[i]);
}

for(i = 0; i < n; i++)
sum = sum + arr[i];
printf(“The sum is %d”,sum);
return 0;
}

// C++ program to find the sum of elements in a given array
#include<iostream>
using namespace std;

int main()
{
//fill the code;
int n;
cout << “Enter the size of array:”;
cin >> n;
int arr[n];
int i, sum = 0;
cout << “Enter the elements of array:”;
for(i = 0; i < n; i++)
{
cin >> arr[i];
}

for(i = 0; i < n; i++)
sum = sum + arr[i];
cout << “The sum is ” << sum;
return 0;
}

// Java program to find sum of elements of given array
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print(“Enter the number of elements:”);
n = s.nextInt();
int a[] = new int[n];
System.out.println(“Enter the array elements:”);
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
sum = sum + a[i];
}
System.out.println(“The sum is:”+sum);
}
}

# Python program to find the sum of elements of given array
arr = [1,2,3]
print(“The sum is”, sum(arr))

#include<stdio.h>
#include<stdlib.h>
int sumOfElements(int n,int *a)
{
int i,sum=0;
for(i=0;i<n;i++)
{
sum=sum+*(a+i);
}
return sum;
}

int main()
{
int *a,n,i;
printf(“Enter the size of array:\n”);
scanf(“%d”,&n);
a=(int *)malloc(n*sizeof(int));
printf(“Enter the elements of array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,a+i);
}
printf(“The sum is: %d”,sumOfElements(n,a));
return 0;
}

Output: