w3resource

C Exercises: Display the sum of the series [ 9 + 99 + 999 + 9999 ...]


21. Sum of Series [9 + 99 + 999 + …]

Write a program in C to display the sum of the series [ 9 + 99 + 999 + 9999 ...].

This C program calculates and displays the sum of a series where each term is formed by repeating the digit 9 (e.g., 9, 99, 999, 9999, ...). The program prompts the user for the number of terms (n) and then computes the sum using a loop that generates each term and adds it to the total sum. The final sum is then printed to the console.

Visual Presentation:

Display the sum of the series [ 9 + 99 + 999 +  9999 ...]

Sample Solution:

C Code:

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

void main()
{
    long int n, i, t = 9; // Declare variables to store input, control loop indices, and temporary value.

    int sum = 0; // Initialize a variable to store the sum.

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

    for (i = 1; i <= n; i++) // Loop for the number of terms.
    {
        sum += t; // Add 't' to the sum.
        printf("%ld   ", t); // Print the current value of 't'.
        t = t * 10 + 9; // Update 't' for the next iteration.
    }

    printf("\nThe sum of the series = %d \n", sum); // Print the sum of the series.
}

Output:

Input the number or terms :5                                                                                  
9   99   999   9999   99999                                                                                   
The sum of the series = 111105  

Flowchart:

Flowchart: Display   the sum of the series [ 9 + 99 + 999 +  9999 ...]

For more Practice: Solve these Related Problems:

  • Write a C program to generate the series 9, 99, 999, ... for n terms and compute their product.
  • Write a C program to display the series 9, 99, 999, ... using string manipulation and then sum the series.
  • Write a C program to calculate the sum of the series using arithmetic progression logic for digit repetition.
  • Write a C program to generate the series and compute the average of the generated terms.

Go to:


PREV : Pyramid Pattern with Odd Asterisks.
NEXT : Floyd's Triangle.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.