harish reddy

harish reddy

  • 2k
  • 162
  • 22.6k

Console readline query

Jun 3 2017 9:17 AM
Console.WriteLine("Program to calculate weight of man on moon by given weight on earth");
Console.WriteLine("Enter mass on earth");
Console.ReadLine();
int m1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter gravity on earth");
int g1 = int.Parse(Console.ReadLine());
int weightonearth = m1 * g1;
Console.WriteLine("Weight on earth is " + " " + weightonearth);
Console.ReadLine();
Console.WriteLine("Now lets calculate weight on moon based on weight on earth");
double weightonmoon = 0.17 * weightonearth;
Console.WriteLine("Weight on moon is " + " " + weightonmoon);
The above program is throwing exception error . Why?
but, if i give like below its not throwing error.
Console.WriteLine("Program to calculate weight of man on moon by given weight on earth");
Console.WriteLine("Enter mass on earth");
string a= Console.ReadLine();
int m1 = int.Parse(a);
Console.WriteLine("Enter gravity on earth");
int g1 = int.Parse(Console.ReadLine());
int weightonearth = m1 * g1;
Console.WriteLine("Weight on earth is " + " " + weightonearth);
Console.ReadLine();
Console.WriteLine("Now lets calculate weight on moon based on weight on earth");
double weightonmoon = 0.17 * weightonearth;
Console.WriteLine("Weight on moon is " + " " + weightonmoon);
 
In this way if it is correct then int g1 = int.Parse(Console.ReadLine()); should also throw me exception error right? But its not throwing error.
One more question is : does console.readline always assume the datatype as string?

Answers (1)