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:
Previous: Write a program in C to display the n terms of square natural number and their sum.
Next: Write a c program to check whether a given number is a perfect number or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join