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.
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); } }
0 comments:
Post a Comment