w3resource
C# Sharp Programming Exercises

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:

C# Sharp Exercises: Calculate the sum of the series 1 +11 + 111 + 1111 + .. n terms

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:

Flowchart : Calculate the sum of the series 1 +11 + 111 + 1111 + .. n terms

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?



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