Program to find the sum of all elements in the array.

Here, we have a basic program example to print the sum of array elements using different languages. This program is created in c language, c++, Java, and Python.

Code to display the sum of array elements in C language

#include <stdio.h>
void  main()
{

    int i,n, sum=0;
       printf("\n\nEnter the size of the array\n");
       scanf("%d",&n);
   int arr[n];
    printf("Input elements in the array :\n");
    for(i=0; i<n; i++)
    {
	    printf("element - %d : ",i);
        scanf("%d", &arr[i]);
    }

     for(i=0; i<n; i++)
    {
        sum += arr[i];
    }

    printf("Sum of all elements stored in the array is : %d\n\n", sum);
}

Code to display the sum of array elements in C++ language

#include <iostream>
using namespace std;

int main()
{
   int i,n,sum=0;
       cout<<"\nEnter the size of the array\n";
       cin>>n;
   int arr[n];
    cout<<"Input elements in the array :\n";
    for(i=0; i<n; i++)
    {
	    cout<<"element " << i <<" - ";
        cin>>arr[i];
    }

    for(i=0; i<n; i++)
    {
        sum += arr[i];
    }

    cout<<"Sum of all elements stored in the array is : "<< sum;
}

Code to display the sum of array elements in Python language

a=[]
sum=0
n=int(input("Enter the size of the array: "))
print("Input elements in the array :")
for i in range(0,n):
    l=int(input())
    a.append(l)

for i in range(0,n):
    sum+=a[i]
print("Sum of all elements stored in the array is :  ",sum)

Code to display the sum of array elements in Java language

import java.util.*;
public class arrays
{
  public static void main(String[] args)
   {
     int n,sum=0;
     Scanner sc = new Scanner(System.in);
     System.out.print("Enter size of the array : ");
     n=sc.nextInt();

    int[] arr = new int[n];

    System.out.println("Enter the elements of the array: ");
    for(int i=0; i<n; i++)
      {
       arr[i]=sc.nextInt();
       }
     for(int i=0; i<n; i++)
    {
        sum += arr[i];
    }

   System.out.println("Sum of all elements stored in the array is : "+sum);
   }
}