C# program to find sum and average of n numbers entered by user using foreach loop

Category > CSHARP || Published on : Wednesday, November 18, 2020 || Views: 1094 || find sum and average of n numbers entered by user using foreach loop


Here Pawan Kumar will explain how to find sum and average of n numbers entered by user using foreach loop

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

namespace ConsoleApplication24
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, y;
            float sum = 0;
            float avg;

            Console.WriteLine("Enter value of n: ");
            String no = Console.ReadLine();
            n = Convert.ToInt32(no);
            int[] nums = new int[n];
            Console.WriteLine("Enter value in array: ");
            for (int i = 0; i < n; i++)
            {

                Console.WriteLine("Enter value: ");
                String z = Console.ReadLine();
                y = Convert.ToInt32(z);

                nums[i] = y;
            }
            foreach (int x in nums)
            {
                Console.WriteLine("Value is: " + x);
                sum = sum + x;
            }
            Console.WriteLine("Summation is: " + sum);
            avg = sum / n;
            Console.WriteLine("Avreage is: " + avg);
            Console.ReadLine();
        }
    }
}