Arithmetic Progression in C#

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Collections.Generic;

public static class ArithmeticProgression
{
	public static List<int> GetTerms(int start, int step, int term_count){
		var terms = new List<int>();
		var next_term = start;

		do
		{
			terms.Add(next_term);
			next_term += step;

		} while (terms.Count() < term_count);

		return terms;
	}
}	

public class Program
{
	public static void Main()
	{
		Console.WriteLine("Arithmetic Progression in C#\n");
		
		RunDemo();
		RunDemo(2, 3, 6);		
		RunDemo(1, 4, 5);
		RunDemo(13, 7, 15, false);
		RunDemo(133, 74, 45, false);
	}
	
	public static void RunDemo(int start=1, int step=1, int term_count=10, bool showEachTerm = true)
	{
		var terms = ArithmeticProgression.GetTerms(start, step, term_count);

		var mainSectionStart = "".PadRight(33, '=');
		var mainSection = "".PadRight(33, '-');
		var subSection = "".PadRight(20, '-');
		var subSectionEnd = "".PadRight(20, '=');

		Console.WriteLine(
		    $"{mainSectionStart}\n" +
		    $"- Arithmetic Progression Options:\n" +
		    $"{mainSection}\n" +
		    $"- Start at:\t\t{start}\n" +
		    $"- Increase by:\t\t{step}\n" +
		    $"- Number of Terms:\t{term_count}\n" +
		    $"- ShowEachTerm:\t{showEachTerm}\n" +
		    $"{subSection}\n" +
		    $"- Sum of Terms:\t{terms.Sum()}");
		
		if (showEachTerm)
		{			
			Console.WriteLine(
			    $"{subSection}\n" +
			    $"- List of Terms:\n{subSection}");
			
			terms.ForEach(term => Console.WriteLine($"  {term}"));
		}

		Console.WriteLine($"{subSectionEnd}\n\n");
	}

}
;