Kodewick
Start learning →
Starter←→0 / 50

Sum a + aa + aaa

#027Starterterm * 10 + a5 min

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.

Examples
sum_a(2, 5)→24690
sum_a(1, 3)→123
The functionlong long sum_a(int a, int n)
main.c
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;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.