---
title: "Series 1+11+111+… by recursion"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:series-1-11-111-recursive"
source: https://www.bctengineer.com/q/semester_1/computer_programming/series-1-11-111-recursive
site: BCT Engineering
---

# Series 1+11+111+… by recursion

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2072 Kartik, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/series-1-11-111-recursive
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

Write a program to calculate the sum of the series 1+11+111+…….+up to N terms using recursive function. If N is read as 5, the series is: 1+11+111+1111+11111.

## Short answer

Use two recursive functions: term(i) returns 1 if i==1 else term(i-1)*10+1, and sumSeries(n) returns 0 if n==0 else sumSeries(n-1)+term(n). For n=5, sum=12345.

## Answer

**Idea.** Each term is made from the previous one: term(1) = 1 and term(i) = term(i−1) × 10 + 1 (1 → 11 → 111 → …). The sum is also recursive: sum(N) = sum(N−1) + term(N), with sum(0) = 0.

Two recursive functions are used:
- `term(i)` gives the i-th term (base case i = 1).
- `sumSeries(n)` adds term(n) to the sum of the first n−1 terms (base case n = 0).

```c
#include <stdio.h>

/* term(i) = i-th term: 1, 11, 111, ... */
long term(int i)
{
    if (i == 1)
        return 1;
    return term(i - 1) * 10 + 1;
}

/* sum of first n terms */
long sumSeries(int n)
{
    if (n == 0)
        return 0;
    return sumSeries(n - 1) + term(n);
}

int main(void)
{
    int n;
    printf("Enter N: ");
    scanf("%d", &n);
    printf("Sum of first %d terms = %ld\n", n, sumSeries(n));
    return 0;
}
```

**Sample run**

Input: `5`

```text
Enter N: Sum of first 5 terms = 12345
```

The sum 1 + 11 + 111 + 1111 + 11111 = 12345 is correct. `long` is used because the terms grow quickly; with a 32-bit `long`, N = 11 or more overflows (the 11th term 11111111111 is larger than 2147483647), so use `long long` for larger N.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is each term of 1, 11, 111, ... obtained from the previous one?
   A. term(i) = term(i-1) + 10
   B. term(i) = term(i-1) * 11
   C. term(i) = term(i-1) + 11
   D. term(i) = term(i-1) * 10 + 1
   Answer: D. Multiplying by 10 shifts digits left and adding 1 appends a new 1. (Key point: Term relation)

2. Which recursion gives the sum of the first n terms?
   A. sum(n) = sum(n-1) * term(n)
   B. sum(n) = sum(n-1) + term(n), with sum(0) = 0
   C. sum(n) = term(n-1) + term(n)
   D. sum(n) = sum(n+1) - term(n)
   Answer: B. The sum of n terms is the sum of n-1 terms plus the n-th term. (Key point: Recursive sum)

3. What are the base cases of term() and sumSeries()?
   A. term(0) = 1 and sumSeries(1) = 0
   B. term(1) = 0 and sumSeries(0) = 1
   C. both return 1 when the argument is 0
   D. term(1) = 1 and sumSeries(0) = 0
   Answer: D. The first term is 1 and the empty sum is 0. (Key point: Base cases)

4. For N = 5, what does the program print?
   A. 11111
   B. 12345
   C. 123456
   D. 1111
   Answer: B. 1 + 11 + 111 + 1111 + 11111 = 12345. (Key point: Sample result)

## Related in Computer Programming

- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [Cosine series up to n terms](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acosine-series) — Computer Programming, chapter 5
- [Sum of digits using recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-digits-recursive) — Computer Programming, chapter 6
- [Sum of series Σ1/n²](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aseries-sum-1-n-squared) — Computer Programming, chapter 5
- [Recursive function and how it works](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Arecursive-function-explain) — Computer Programming, chapter 6, asked 3×
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6

## Related in other subjects

- [Is recursion slow; recursive vs non-recursive](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Arecursion-slow-compare) — Data Structure and Algorithm, chapter 5
- [Recursion; iterative vs recursive choice](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adefine-recursion-iterative-vs-recursive) — Data Structure and Algorithm, chapter 5
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [Normal vs recursive function](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Anormal-vs-recursive-function) — Data Structure and Algorithm, chapter 5
