C# Sharp Exercises: Calculate the sum of the series 1 +11 + 111 + 1111 + .. n terms
C# Sharp For Loop: Exercise-26 with Solution
Write a program in C# Sharp to find the sum of the series 1 +11 + 111 + 1111 + .. n terms.
Pictorial Presentation:
Sample Solution:
C# Sharp Code:
using System;
public class Exercise26
{
public static void Main()
{
int n,i,sum=0;
int t=1;
Console.Write("\n\n");
Console.Write("Calculate the sum of the series 1 +11 + 111 + 1111 + .. n terms:\n");
Console.Write("------------------------------------------------------------------");
Console.Write("\n\n");
Console.Write("Input the numbe of terms : ");
n= Convert.ToInt32(Console.ReadLine());
for(i=1;i<=n;i++)
{
Console.Write("{0} + ",t);
sum=sum+t;
t=(t*10)+1;
}
Console.Write("\nThe Sum is : {0}\n",sum);
}
}
Sample Output:
Calculate the sum of the series 1 +11 + 111 + 1111 + .. n terms: ------------------------------------------------------------------ Input the numbe of terms : 6 1 + 11 + 111 + 1111 + 11111 + 111111 + The Sum is : 123456
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to display the n terms of square natural number and their sum.
Next: Write a C# Sharp Program to check whether a given number is perfect number or not.
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