Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

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.





                         




Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post