Recursive sum method : Function Definition « Language Basics « C# / C Sharp






Recursive sum method

Recursive sum method
public class SumPrices {

  public static double Sum(double[] p, int start, int end) {
    if (start < end)
       return p[start] + Sum(p,start+1,end);
    else return 0;
  }

  public static void Main() {
    double[] prices = {1.3, 13.68, 314.919, 82.827, 363.949};
    System.Console.WriteLine("The sum is {0:C}", Sum(prices,0,prices.Length));
  }
} 

           
       








Related examples in the same category

1.Use a recursive method, travel, to journey from start to finishUse a recursive method, travel, to journey from start to finish
2.Recursive Factorial method.
3.Recursive function in actionRecursive function in action
4.Define functionDefine function
5.Catch StackOverflowException for recursive function