In C#, Find Square Root Of Positive Number With Exception Handling

using System;

namespace cs1
{
    class FindSquareRoot
    {
        static void Main(string[] args)
        {
			double num, sqrt;
            Console.Write("Find square root of: ");
			string input = Console.ReadLine();
			try{
				num = double.Parse(input);
				if(0 > num) throw new ApplicationException("Number can't be negative.");
				sqrt = Math.Sqrt(num);
				Console.WriteLine("Square root is {0}", sqrt);
			} catch (FormatException e){
				Console.WriteLine("The input should be a number.");
				sqrt = 0;
			} catch (ApplicationException e){
				Console.WriteLine(e.Message);
				sqrt = 0;
			}
        }
    }
}
Download

Comments

Popular posts from this blog