Given an array of integers, find the sum of its elements.
For example, if the array ar=[1,2,3] ,1+2+3=6, so return 6 .
Function Description
Complete the simpleArraySum function in the editor below. 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.
Constraints
0<n, ar[i]<=1000
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
Explanation
We print the sum of the array’s elements: 1+2+3+4+10+11=31
A Very Big Sum HackerRank Solution 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; }
A Very Big Sum HackerRank Solution 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); } }
A Very Big Sum HackerRank Solution in Python
import math import sys ans = 0 for linenum, testnum in enumerate(sys.stdin): if linenum == 1: for num in testnum.split(): ans += int(num) print ans
A Very Big Sum HackerRank Solution in C#
using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { int N = Convert.ToInt32(Console.ReadLine()); string[] array = Console.ReadLine().Split(' '); double sum = 0; for (int i = 0; i < N; i++) sum += Convert.ToInt32(array[i]); Console.WriteLine(sum); } }
Attempt – A Very Big Sum HackerRank Challenge
Link: https://www.hackerrank.com/challenges/a-very-big-sum
Next Challenge – Diagonal Difference HackerRank Solution
Link: https://exploringbits.com/diagonal-difference-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.