Simple Array Sum

Problem Statement

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

Input Format
The first line of the input consists of an integer N. The next line contains N space-separated integers describing the array.

Constraints
1N1000
0A[i]1000

Output Format
Output a single value equal to the sum of the elements of the array.

Sample Input

6
1 2 3 4 10 11

Sample Output

31

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

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define max 1000
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n,temp,sum;
    cin>>n;
    while(n--){
        cin>>temp;
        sum+=temp;
    }
        cout<<sum;
    return 0;
}


Solve me second

Problem Statement

You learnt about STDIN and STDOUT in Solve me first.

This is the second challenge in the introduction series. The purpose of this challenge is to give you a working I/O template in your preferred language. It includes scanning two space-separated integers from STDIN in a loop over T lines, calling a function, returning a value, and printing it to STDOUT.

A pseudo code looks like the following:

read T
loop from 1 to T
    read A and B
    compute the sum
    print value in a newline
end loop

The task is to scan two numbers from STDIN, and print the sum A+B on STDOUT. The code has already been provided for most of the popular languages. This is primarily for you to read and inspect how the IO is handled.

Note: The code has been saved in a template, which you can submit if you want. Or, you may try rewriting it and building it up from scratch.

Input Format
(This section specifies the Input Format.)
The first line contains T (number of test cases) followed by T lines
Each line contains A and B, separated by a space.

As you can see that we have provided in advance the number of lines, we discourage the use of scanning till EOF as not every language has an easy way to handle that. In fact, every HackerRank challenge is designed in such a way that multitests begin with a T line to indicate the number of lines.

Output Format
(This section specifies the Output Format.)
An integer that denotes Sum (A+B) printed on new line for every testcase.

Constraints
(This section tells what input you can expect. You can freely assume that the input will remain within the boundaries specified.)
1T,A,B1000

Sample Input

2
2 3
3 7

Sample Output

5
10

The above sample should be taken seriously. 2 in the first line describes how many lines will follow, and your test cases are 2, 3 and 3, 7 in two separate lines. Your output should be 5 and 10 printed on two separate lines. If you print extra lines or “The answer is: 5”, any such extra characters in output will result in a Wrong Answer, as the judging is done using diff checker.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int solveMeSecond(int a, int b) {
  return a+b;
}

int main() {
  int num1, num2;
  int sum;
  int t;
  cin >> t;

  for (int i=0; i> num1 >> num2;
    sum = solveMeSecond(num1,num2);
    cout << sum << endl;
  }

  return 0;
}


Solve me first

Problem Statement

This is an introductory challenge. The purpose of this challenge is to give you a working I/O template in your preferred language. It includes scanning two integers from STDIN, calling a function, returning a value, and printing it to STDOUT.

The task is to scan two numbers from STDIN, and print the sum A+B on STDOUT. The code has already been provided for most of the popular languages. This is primarily for you to read and inspect how the IO is handled.

Note: The code has been saved in a template, which you can submit if you want. Or, you may try rewriting it and building it up from scratch.

Input Format
(This section specifies the Input Format.)
Given A and B on two different lines.

Output Format
(This section specifies the Output Format.)
An integer that denotes Sum (A+B)

Constraints
(This section tells what input you can expect. You can freely assume that the input will remain within the boundaries specified. As an example here given below, A and B will never be below 1 or above 1000.)
1A,B1000

Sample Input

2
3

Sample Output

5

The above sample should be taken seriously. The input will be 2 and 3 in two separate lines, and the output should be just one number, 5. You should not print any whitespace at the beginning of output (e.g. ” 5″ or “\n5”), unless specifically asked for. Also, printing any extra non-whitespace characters such as “The answer is: 5” will result in a Wrong Answer, as the judging is done using diff checker.

Solution

  
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


    static int solveMeFirst(int a, int b) {
        return a+b;
   }

   
 public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int _a;
        _a = in.nextInt();
        int _b;
        _b = in.nextInt();
        int sum;
        sum = solveMeFirst(_a, _b);
        System.out.println(sum);
   }
}