public static void Main()
CalculateYield(0, 2, 150, 40, 0, 0);
public static void CalculateYield(double initialSum, double interestRate, double addedValue, int numberOfYears, double agressiveAmount, int agressiveYears)
double cumulusForAddedValue = 0;
double initialSumValue = initialSum;
Console.WriteLine("\n\tInitial investment: {0}\n\n\t6-month payments: {1}, total years: {2}\n\n\tagressive payment: {3}, number of agressive years: {4}\n", initialSum, addedValue, numberOfYears, agressiveAmount, agressiveYears);
double correctInvestedCalculation = 0;
for (int i = 1; i <= numberOfYears; i++)
double halfYearlyInvestment = agressiveYears < i ? addedValue : agressiveAmount;
cumulusForAddedValue += cumulusForAddedValue * interestRate / 100 + halfYearlyInvestment;
cumulusForAddedValue += cumulusForAddedValue * interestRate / 100 + halfYearlyInvestment;
initialSum += initialSum * interestRate / 100;
initialSum += initialSum * interestRate / 100;
correctInvestedCalculation = agressiveYears > i ? agressiveAmount * 2 * i : agressiveAmount * agressiveYears * 2 + addedValue * (i - agressiveYears) * 2;
sum = initialSum + cumulusForAddedValue;
DisplayYield(i, numberOfYears, agressiveYears, initialSum, sum, initialSumValue, addedValue, agressiveAmount, cumulusForAddedValue, correctInvestedCalculation);
public static void DisplayYield(int year, int numberOfYears, int agressiveYears, double initialSum, double sum, double initialSumValue, double addedValue, double agressiveAmount, double cumulusForAddedValue, double correctInvestedCalculation)
double roundedSum = Math.Floor(sum);
Console.Write(" | {0}: ", FormatNumber(2, year));
Console.Write(" ONE: {0}, PROFIT: {1} ", FormatNumber(roundedSum, Math.Floor(initialSum)), FormatNumber(roundedSum, (int)Math.Floor(initialSum - initialSumValue)));
if (addedValue != 0 || (agressiveAmount != 0 && agressiveYears != 0))
Console.Write(" MTH : {0}, PROFIT: {1} ", FormatNumber(roundedSum, Math.Floor(cumulusForAddedValue)), FormatNumber(roundedSum, Math.Floor(cumulusForAddedValue - correctInvestedCalculation)));
if ((addedValue != 0 || (agressiveAmount != 0 && agressiveYears != 0)) || initialSum == 0)
Console.Write(" SUM: {0} ", FormatNumber(roundedSum, roundedSum));
if (year == numberOfYears)
double invested = initialSumValue + correctInvestedCalculation;
Console.WriteLine("\n\n\n\t\t\tINVESTED: {0} | | EARNED: {1} | | SUM: {2}\n\n\n", invested, Math.Floor(sum - invested), roundedSum);
public static string FormatNumber<Tnumber>(Tnumber maxNumber, Tnumber number)
int amountOfSpaces = maxNumber.ToString().Length + 1;
string stringified = number.ToString();
string emptyString = new string (' ', amountOfSpaces - stringified.Length);
return stringified.Length < amountOfSpaces ? emptyString + stringified : stringified;