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:
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?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework