April 8, 2020

How to write a program to print sum and average number in C#

Write a program in C# Sharp to read 5 numbers enter by user and find their sum and average.


How to write a program to print sum and average number in C#




Like user Enter 1,2,3,4,5

The sum of 5 number is

1+2+3+4+5 = 15

The Average is

15/5 = 3


Sample Solution

C# Sharp Code

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1, n2, n3, n4, n5, sum, avg;
            Console.WriteLine("Enter Five number");
            n1 = Convert.ToInt32(Console.ReadLine());
            n2 = Convert.ToInt32(Console.ReadLine());
            n3 = Convert.ToInt32(Console.ReadLine());
            n4 = Convert.ToInt32(Console.ReadLine());
            n5 = Convert.ToInt32(Console.ReadLine());
            sum = (n1 + n2 + n3 + n4 + n5);
            avg = (sum / 5);
            Console.WriteLine("The sum is: {0}", sum);
            Console.WriteLine("The Avg is: {0}", avg);
            Console.ReadLine();

        }
    }
}

Sample Output

The Sum is: 15

The Avg is: 3

How to write a program to print sum and average number in C#

No comments:

Post a Comment