fork download
  1. /*
  2.   Problem: Sum of Array Elements
  3.  
  4.   You are given T test cases. For each test case, you are given an array of N integers.
  5.   Your task is to compute and print the sum of the elements of the array for each test case.
  6.  
  7.   Input Format:
  8.   - The first line contains a single integer T — the number of test cases.
  9.   - For each test case:
  10.   - The first line contains an integer N — the size of the array.
  11.   - The second line contains N space-separated integers — the elements of the array.
  12.  
  13.   Output Format:
  14.   - For each test case, output a single line containing the sum of the array elements.
  15.  
  16.   Constraints:
  17.   1 <= T <= 1000
  18.   1 <= N <= 1000
  19.   1 <= a[i] <= 1000
  20.  
  21.   Example Input:
  22.   2
  23.   5
  24.   1 2 3 4 5
  25.   3
  26.   10 20 30
  27.  
  28.   Example Output:
  29.   15
  30.   60
  31. */
  32.  
  33. #include <iostream>
  34. using namespace std;
  35.  
  36. int main() {
  37. ios::sync_with_stdio(false);
  38. cin.tie(nullptr);
  39.  
  40. int T;
  41. cin >> T;
  42.  
  43. while (T--) {
  44. int N;
  45. cin >> N;
  46.  
  47. int sum = 0;
  48. for (int i = 0; i < N; ++i) {
  49. int num;
  50. cin >> num;
  51. sum += num;
  52. }
  53.  
  54. cout << sum << '\n';
  55. }
  56.  
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0.01s 5320KB
stdin
2
5
1 2 3 4 5
3
10 20 30
stdout
15
60