C# : Program to calculate sum and average of two numbers

In this simple C# tutorial we calculate sum of three numbers given by the user. Here we take "float" datatype and five variables as the number entered by
the user may be integer as well as float, three variables "a", "b", "c" for numbers entered by the user, "sum" to calculate sum of these three numbers and "av" for average. The Console.ReadLine() statement is used to accept input from the user and store it in variables a,b and c. The Convert.ToInt32 is used to convert the data entered by the user to the int data type because the Console.ReadLine() reads the data in string format.

using System;
class sum
{
 public static void Main(String []args)
 {
        float a,b,c,sum,av;
        Console.WriteLine("Enter three numbers:");
        a=Convert.ToInt32(Console.ReadLine());
        b=Convert.ToInt32(Console.ReadLine());
        c=Convert.ToInt32(Console.ReadLine());
        //to calculate sum of three nos
        sum=a+b+c;
        //to calculate average
        av=sum/3;
        Console.WriteLine("\nSUM=" + sum);
        Console.WriteLine("\nAverage=" +av);
 }
}
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment