This tutorial will demonstrate how to read from the input console (console line) to answer the prompts. The prompts will need to be a integer value to add the two inputs together.
I am using the System.Console object to read from the console and then converting the string inputs into integers with the Convert.ToInt32 function (the System.Convert has many methods and ToInt32 is one of these).
The code
using System; // for the console object class addtwonumbers { public static void Main() { try { // output to the console Console.Write("Please enter value 1 : "); // readin the console input, and then convert to a integer value int val1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Please enter value 2 : "); int val2 = Convert.ToInt32(Console.ReadLine()); // write out the answer Console.WriteLine("Answer = " + (val1 + val2)); } catch (Exception e) { // any errors. Like converting a string to a integer value Console.WriteLine("Error : " + e.ToString()); } } } |
save that as addtwonumbers.cs.
Once compiled (mcp addtwonumbers.cs with mono) and executed (mono addtwonumbers.exe) the output will be
Please enter value 1 : 30 Please enter value 2: 23 Answer = 53
class addtwonumbers
{
public static void Main(string[] args)
{
int a,b,c;
System.Console.WriteLine(“Enter the value of a=”);
a=int.Parse(System.Console.ReadLine());
System.Console.WriteLine(“Enter the value of b=”);
b=int.Parse(System.Console.ReadLine());
c=a+b;
System.Console.WriteLine(“Sum is “+c);
}
}
thanks to sudeep kumar………for giving code in easy way to understand
good code easy to understand
Thank you sudeep
i want some good examples
give some how to display names in c#
tnx a lot 🙂
Thanx 🙂
what is .parse??
.parse is a way to convert a string into a int (in this case).