a is one digit (0–9). Work out s = a + aa + aaa + …, with n terms in all, where term i is made of i copies of a. Write sum_a(a, n). With a = 2, n = 5: 2 + 22 + 222 + 2222 + 22222 = 24690. The numbers get big, so use long long.
sum_a(2, 5)→24690sum_a(1, 3)→123long long sum_a(int a, int n)long long sum_a(int a, int n) {
long long term = 0, s = 0;
for (int i = 1; i <= n; i++) {
// your code here: add one more a to the end of term, then add it to s
}
return s;
}
Run tries the examples. When they look right, Submit.