Q:

Write a C# Sharp program to calculate true mean value, mean with rounding away from zero and mean with rounding to nearest of some specified decimal values

0

Write a C# Sharp program to calculate true mean value, mean with rounding away from zero and mean with rounding to nearest of some specified decimal values. 
Expected Output:
True Mean: 16.36
Away From Zero: 16.37
Rounding to Nearest: 16.35

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
using System.Text;
namespace exercises {
  class Program {
    static void Main(string[] args) {
      decimal[] values = {
        10.23m,
        10.27m,
        14.34m,
        11.15m,
        41.51m,
        10.65m
      };
      decimal sum = 0;
      // Calculate true mean.
      foreach(var value in values)
      sum += value;
      Console.WriteLine("True Mean: {0:N2}", sum / values.Length);
      // Calculate mean with rounding away from zero.
      sum = 0;
      foreach(var value in values)
      sum += Math.Round(value, 1, MidpointRounding.AwayFromZero);
      Console.WriteLine("Away From Zero: {0:N2}", sum / values.Length);
      // Calculate mean with rounding to nearest.
      sum = 0;
      foreach(var value in values)
      sum += Math.Round(value, 1, MidpointRounding.ToEven);
      Console.WriteLine("Rounding to Nearest: {0:N2}", sum / values.Length);
    }
  }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now