1.9 – Average

Question


Write a program to accept 3 numbers and calculate the average.

Solution


class Average
    {
        static void Main(string[] args)
        {
            int a, b, c;
            float avg;
            Console.Write(“Enter first number : “);
            a = Convert.ToInt16(Console.ReadLine());
            Console.Write(“Enter second number : “);
            b = Convert.ToInt16(Console.ReadLine());
            Console.Write(“Enter third number : “);
            c = Convert.ToInt16(Console.ReadLine());            avg = (float)(a + b + c) / 3.0f;            //Typecasting
            Console.WriteLine(“Average of given numbers is {0:f}”, avg); //{0:f} means decimal upto 2 places
        }
    }

 Previous                                                Home                                                   Next


Output

9