w3resource
C# Sharp Programming Exercises

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

C# Sharp For Loop: Exercise-21 with Solution

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

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise21 
{  
    public static void Main()
{   
    int n,i,t=9;
	int sum =0;
	
	Console.Write("\n\n");
    Console.Write("Display the sum of the series [ 9 + 99 + 999 + 9999 ...]:\n");
    Console.Write("-----------------------------------------------------------");
    Console.Write("\n\n");  	
	
	Console.Write("Input the number or terms :");
    n= Convert.ToInt32(Console.ReadLine());  	
	for (i=1;i<=n;i++)
	{ sum +=t;
	  Console.Write("{0}   ",t);
	  t=t*10+9;
	}
	Console.Write("\nThe sum of the saries = {0} \n",sum);
  }	
}  

Sample Output:

                                  
Display the sum of the series [ 9 + 99 + 999 + 9999 ...]:                                                                                                      
-----------------------------------------------------------                            
Input the number or terms :5                                                                                                         
9   99   999   9999   99999                                                                                                         
The sum of the saries = 111105

Flowchart:

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

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to display the pattern like a pyramid using asterisk and each row contain an odd number of asterisks.
Next: Write a program in C# Sharp to print the Floyd's Triangle.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming