Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,824 answers

573 users

How to calculate sum of series 1 + 11 + 111 + 1111 + ... N in C#

1 Answer

0 votes
using System;

// sum = 1 + 11 + 111 + 1111 + ... N 

public class Program
{
	private static int sum_of_series(int N) {
		int sum = 0;
		int tmp = 1;

		for (int i = 0; i < N; i++) {
			Console.Write(tmp + " ");

			if (i < N - 1) {
				Console.Write("+ ");
			}

			sum += tmp;
			tmp = (tmp * 10) + 1;
		}

		return sum;
	}
	public static void Main(string[] args)
	{
		int N = 6;

		int sum = sum_of_series(N);

		Console.WriteLine("= " + sum);
	}
}





/*
run:
      
1 + 11 + 111 + 1111 + 11111 + 111111 = 123456
   
*/

 





answered Jan 17 by avibootz
...