1.6 Use of Placeholders {0}
Question
Write a program to enter your name and display in specific format –You are (your name)- for ex. You are Rohit Patil
Accept two numbers and print their sum, it might look like this . Addition of 4 and 5 is 9.
Solution
class Placeholders
{
static void Main(string[] args)
{
// program to display name
String name;
Console.Write(“Enter your name: “);
name = Console.ReadLine();
Console.WriteLine(“You are {0}”, name); //{0} this is placeholder // addition of two numbers
int a, b, c; //initialization
Console.Write(“Enter first number : “);
a = Convert.ToInt16(Console.ReadLine());
Console.Write(“Enter second number : “);
b = Convert.ToInt16(Console.ReadLine());
c = a + b; //calculation for addition
Console.WriteLine(“Addition of {0} and {1} is {2}”,a,b,c); //use of placeholder, {0} will replace by value of a, similarly {1} by b and {2} by c
}
}
{
static void Main(string[] args)
{
// program to display name
String name;
Console.Write(“Enter your name: “);
name = Console.ReadLine();
Console.WriteLine(“You are {0}”, name); //{0} this is placeholder // addition of two numbers
int a, b, c; //initialization
Console.Write(“Enter first number : “);
a = Convert.ToInt16(Console.ReadLine());
Console.Write(“Enter second number : “);
b = Convert.ToInt16(Console.ReadLine());
c = a + b; //calculation for addition
Console.WriteLine(“Addition of {0} and {1} is {2}”,a,b,c); //use of placeholder, {0} will replace by value of a, similarly {1} by b and {2} by c
}
}
Previous Home Next
Output