How to take input from USER'S in C#

0
How to take input from USER'S in C#:-


To take input from User's in C# We will use Console.ReadLine(),  It will return data in String pattern which can be easily typecast into different datatype.


C# provide parse() and Convert class to data type conversion where parse() throw ArgumentNullException if data is Null but Convert Class provides no any exception if assigned String value is Null.

using System;
class Add
{
   static void Main()
   {
       int a,b,c;
       Console.WriteLine("Enter First Number");
       a = Convert.ToInt32(null);
      
       Console.WriteLine("Enter Second Number"); 
       b = int.Parse(Console.ReadLine());  
       c=a+b;
       Console.WriteLine("Result is {0}",c);
   }

}

User's Input based on Different Datatype:-

1)    int a =  int.Parse(Console.ReadLine())   // String to integer

      int a = Convert.ToInt32(Console.ReadLine())   



2)    float b = float.Parse(Console.ReadLine())

3)    String s =   Console.ReadLine()

4)    char ch = char.Parse(Console.ReadLine())


Complete Program Explanation to take input from users.

using System;   //namespace which contains predefined classes
class SI
{

  public static void Main()
  {
      float p,r,t,si;
      Console.WriteLine("Enter value for P");
      p=float.Parse(Console.ReadLine());
      Console.WriteLine("Enter value for R");
      r=float.Parse(Console.ReadLine());
      Console.WriteLine("Enter value for T");
      t=float.Parse(Console.ReadLine());
      si= (p*r*t)/100;
   
      Console.WriteLine("result is " + si);
  }


}




//  float.Parse() is the predefine method which is used to convert float String to float value.





                         




Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)