Simple Array Sum : HackerRank Problem Solution

You are given an array of integers of size N. You need to print the sum of the elements of the array.


Input:

6

1 2 3 4 10 11

Output:


31


Explanation:


1 + 2 + 3 + 4 + 10 + 11 =31


C Implementation:


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int n,x,i,sum=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {
        scanf("%d",&x);
        sum+=x;
    }
    printf("%d",sum);
    return 0;
}

Java Implementation:

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeRegister
{
 public static void main (String[] args) throws java.lang.Exception
 {
  Scanner in = new Scanner(System.in);
  int n = in.nextInt();
  int sum = 0;
  while(n>0)
  {
   int x = in.nextInt();
   sum += x;
   n--;
  }
  System.out.print(sum);
 }
}