﻿ C : Calculate the sum of the series 1 +11 + 1111 + ... N

# C Exercises: Calculate the sum of the series 1 +11 + 111 + 1111 + .. n terms

## C For Loop: Exercise-26 with Solution

Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms.

Visual Presentation: Sample Solution:

C Code:

``````#include <stdio.h> // Include the standard input/output header file.

int main() {
int n, i;  // Declare variables for loop control and input.
long sum = 0; // Initialize a variable to hold the sum of the series.
long int t = 1; // Initialize a variable to generate the series.

printf("Input the number of terms : "); // Prompt the user for input.
scanf("%d", &n); // Read the value of 'n' from the user.

for(i = 1; i <= n; i++) // Loop to generate and sum the series.
{
printf("%ld  ", t); // Print the current term of the series.

if (i < n)
{
printf("+ "); // Print addition sign between terms (except the last term).
}

sum = sum + t; // Add the current term to the running sum.
t = (t * 10) + 1; // Generate the next term of the series.
}

printf("\nThe Sum is : %ld\n", sum); // Print the final sum of the series.

return 0; // Return 0 to indicate successful execution.
}

```
```

Sample Output:

```Input the number of terms : 5
1 + 11 + 111 + 1111 + 11111
The Sum is : 12345
```

Flowchart: C Programming Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿