Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Write a program that reads an integer number and calculates and prints its square root. If the number is invalid or negative, print "Invalid number". In all cases finally print "Good bye". Use try-catch-finally.
- */
- using System;
- class SquareRoot
- {
- static void Main()
- {
- try
- {
- SquareRootFromNumber();
- }
- catch (FormatException)
- {
- Console.WriteLine("Invalid number!");
- }
- finally
- {
- Console.WriteLine("Good bye!");
- }
- Console.WriteLine();
- Main();
- }
- private static bool NegativeException(int number)
- {
- if (number < 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private static void SquareRootFromNumber()
- {
- int number;
- double squareRoot;
- Console.Write("Enter integer number: ");
- number = int.Parse(Console.ReadLine());
- if(NegativeException(number) == true)
- {
- throw new FormatException();
- }
- squareRoot = Math.Sqrt(number);
- Console.WriteLine("Square root from {0} = {1}", number, squareRoot);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment