Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SquareRootCalculator
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Please enter a number to find the square root of and then press \'Enter\': ");
- Console.WriteLine("________________________________________________\n");
- bool detector = false;
- double x;
- double userInput = 0;
- while (detector == false)
- {
- if (!double.TryParse(Console.ReadLine(), out x))
- {
- Console.WriteLine("The square root calculator needs a number greater than zero.\n");
- Console.WriteLine("Please enter a number to find the square root of and then press \'Enter\': ");
- }
- else if (x < 0)
- {
- Console.WriteLine("The square root calculator needs a number greater than zero.\n");
- Console.WriteLine("Please enter a number to find the square root of and then press \'Enter\': ");
- }
- else
- {
- detector = true;
- userInput = Convert.ToDouble(x);
- }
- }
- double y = userInput * (userInput * .1);
- double answer;
- for (int i = 0; i < 100; i++)
- {
- answer = 0.5 * (y + (userInput / y));
- // takes output and causes the next iteration of the formula to use the output to calculate the new value
- y = answer;
- }
- Console.WriteLine("\nYour answer is: " + "{0:0.#######}", y);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement