aaaaaaaaaaaaaaaaa
The following program will add and print sum on the output screen.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int i, j, k; //I use 'i,j and k' for Integer value.
Console.Write("Enter first number\n");
i = Int32.Parse(Console.ReadLine()); //user enter value converting to Integer and assigning to 'i'.
Console.Write("Enter 2nd number\n");
j = Int32.Parse(Console.ReadLine()); //user enter value converting to Integer and assigning to 'j'.
k = i + j; //adding 'i' and 'j' values and assigning to 'k'.
Console.Write("The Answer is{0}. ", k); //printing 'k' value.
Console.ReadKey();
}
}
}
The following program will also add and print sum on the output screen.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter first number\n");
int i = Int32.Parse(Console.ReadLine()); //user enter value converting and assigning.
Console.Write("Enter 2nd number\n");
int j = Int32.Parse(Console.ReadLine()); //user enter value converting and assigning.
int k = i + j; //adding 'i' and 'j' values and assigning to 'k'.
Console.Write("The Answer is{0}. ", k); //printing 'k' value, without '{0}' not working.
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string a, b; //'a' and 'b' used for string values.
int i, j, k; //'i', 'j' and 'k' used for integer values.
Console.Write("Enter first number\n");
a = Console.ReadLine(); //read first number 'a' from user as string.
Console.Write("Enter 2nd number\n");
b = Console.ReadLine(); //read second number 'b' from user as string.
i = Int32.Parse(a); //convert numbers from type string'a' to type int'i'.
j = Int32.Parse(b); //convert numbers from type string'b' to type int'j'.
k = i + j; //add numbers 'i' and 'j' and assign to 'k'.
Console.Write("The Answer is: {0}", k);
Console.ReadKey();
}
} //i give first '5' and second '6'.
}
Method Int32.Parse
(a static method of class Int32
) converts its string argument to an integer
Class Int32
is part of the System namespace. it assigns the integer that Int32.Parse
returns
to variable i
. Any subsequent references to i
in the program use this integer value and
assigns the integer that Int32.Parse
returns to variable j
. Any subsequent references
to j
in the program use this integer value. You can eliminate the need for string variables
a
and b
by combining the input and conversion operations.
("The Answer is {0}.", k);
use {0} to indicate a placeholder for a variable’s value. If we
assume that sum contains the value 11
, the expression evaluates as follows: Method WriteLine
encounters a number in curly braces, {0}, known as a format. This indicates that the variable
found after the string in the list of arguments (in this case, k) will be evaluated and
incorporated into our string, in place of the format. The resulting string will be
The Answer is 11
Similarly, in the statement
Console.WriteLine("The numbers entered are {0} and {1}", i, j);
the value of i
would replace
{0} (because it is the first variable) and the value of j
would replace {1} (because it is the
second variable). The resulting string would be The numbers entered are 5 and 6
. More formats
can be used ({2}, {3} etc.) if there are more variables to display in the string.