A Very Big Sum
Problem Statement :
In this challenge, you are required to calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large. Function Description Complete the aVeryBigSum function in the editor below. It must return the sum of all array elements. aVeryBigSum has the following parameter(s): int ar[n]: an array of integers . Return long: the sum of all array elements Input Format The first line of the input consists of an integer n/ . The next line contains n space-separated integers contained in the array. Output Format Return the integer sum of the elements in the array. Constraints 1 <= n <=10 0 <= ar[i] <= 10^10
Solution :
In C :
long aVeryBigSum(int ar_count, long* ar) {
long double sum = 0;
for(int i = 0; i< ar_count; i++)
{
sum = sum + ar[i];
}
return sum;
}
In C ++ :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin>>n;
unsigned long long int sum=0,in;
for(int i=0;i<n;i++)
{cin>>in;
sum+=in;}
cout<<sum;
return 0;
}
In Python :
input()
print(sum(map(int, input().split())))
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
long sum = 0;
while(t-- > 0){
sum += s.nextInt();
}
System.out.println(sum);
}
}