Sum of Series 1 + 11 + 111

Medium Loops
Sum the series 1 + 11 + 111 + ... up to n terms using a loop.

Sample input

4

Sample output

1234

Solution

n = 4
term = 0
total = 0
for _ in range(n):
    term = term * 10 + 1
    total += term
print(total)
const n = 4;
let term = 0, total = 0;
for (let i = 0; i < n; i++) {
  term = term * 10 + 1;
  total += term;
}
console.log(total);
public class Main {
    public static void main(String[] args) {
        int n = 4;
        long term = 0, total = 0;
        for (int i = 0; i < n; i++) {
            term = term * 10 + 1;
            total += term;
        }
        System.out.println(total);
    }
}
fun main() {
    val n = 4
    var term = 0L
    var total = 0L
    for (i in 0 until n) {
        term = term * 10 + 1
        total += term
    }
    println(total)
}
let n = 4
var term = 0, total = 0
for _ in 0..<n {
    term = term * 10 + 1
    total += term
}
print(total)
void main() {
  int n = 4;
  int term = 0, total = 0;
  for (var i = 0; i < n; i++) {
    term = term * 10 + 1;
    total += term;
  }
  print(total);
}
#include <iostream>
using namespace std;

int main() {
    int n = 4;
    long term = 0, total = 0;
    for (int i = 0; i < n; i++) {
        term = term * 10 + 1;
        total += term;
    }
    cout << total << endl;
    return 0;
}
#include <stdio.h>

int main() {
    int n = 4;
    long term = 0, total = 0;
    for (int i = 0; i < n; i++) {
        term = term * 10 + 1;
        total += term;
    }
    printf("%ld\n", total);
    return 0;
}