Solve Me First

Welcome to HackerRank! The purpose of this challenge is to familiarize you with reading input from stdin (the standard input stream) and writing output to stdout (the standard output stream) using our environment.

Review the code provided in the editor below, then complete the solveMeFirst function so that it returns the sum of two integers read from stdin. Take some time to understand this code so you’re prepared to write it yourself in future challenges.

Select a language below, and start coding!

Input Format

Code that reads input from stdin is provided for you in the editor. There are 2

“>22 lines of input, and each line contains a single integer.

Output Format

Code that prints the sum calculated and returned by solveMeFirst is provided for you in the editor.

Solution

#include
#include
#include
#include
#include
using namespace std;

int solveMeFirst(int a, int b) {
return a+b;
}
int main() {
int num1, num2;
int sum;
cin>>num1>>num2;
sum = solveMeFirst(num1,num2);
cout<Simple Array

Given an array of N

“>NN integers, can you find the sum of its elements?

Input Format

The first line contains an integer, N

“>NN, denoting the size of the array.
The second line contains N

“>NN space-separated integers representing the array’s elements.

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, which is: 1+2+3+4+10+11=31

“>1+2+3+4+10+11=311+2+3+4+10+11=31.

Solution

#include
#include
#include
#include
#include
#include

#include
#include
#include
#include
using namespace std;

int main()
{
int n;
int sum = 0;
scanf(“%d”,&n);
while (n–)
{
int a;
scanf(“%d”,&a);
sum+=a;
}
printf(“%d”,sum);
}

A Very Big Sum

You are given an array of integers of size N

“>NN. You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large.

Input

The first line of the input consists of an integer N

“>NN. The next line contains N

“>NN space-separated integers contained in the array.

Constraints
1N10

“>1N101≤N≤10
0A[i]1010

“>0A[i]10100≤A[i]≤1010

Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005

Output
Print a single value equal to the sum of the elements in the array. In the above sample, you would print 5000000015

“>50000000155000000015.

Note: The range of the 32-bit integer is (231) to (2311) or [2147483648,2147483647]

“>(231) to (2311) or [2147483648,2147483647](−231) to (231−1) or [−2147483648,2147483647].
When we add several integer values, the resulting sum might exceed the above range. You might need to use long long int in C/C++ or long data type in Java to store such sums.

Solution

#include
#include
#include
#include
#include
using namespace std;
int main() {

int N; scanf(“%d\n”, &N);
long long sum(0);
while(N–){
long long temp; scanf(“%lld”, &temp);
sum += temp;
}

printf(“%lld\n”, sum);

return 0;
}