Challenge: Add Them Up
Completed
Read n numbers and print their total. The total can be bigger than an int can hold, so use long long. This is an O(n) algorithm.
Input
Line 1: n. Line 2: n whole numbers (each up to 109).
Output
The total.
Example
Input
5 1 2 3 4 5
Output
15
Editor
C++
Hints
Start with long long total = 0.
Read each number and add it.
Solution explanation
Three numbers of 10⁹ add up to 3 × 10⁹ – too big for an int, fine for long long.
Show solution
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
long long total = 0;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
total += x;
}
cout << total << endl;
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module