Advertisement
Guest User

Untitled

a guest
May 25th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SquareRootCalculator
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Console.WriteLine("Please enter a number to find the square root of and then press \'Enter\': ");
  10. Console.WriteLine("________________________________________________\n");
  11. bool detector = false;
  12. double x;
  13. double userInput = 0;
  14. while (detector == false)
  15. {
  16. if (!double.TryParse(Console.ReadLine(), out x))
  17. {
  18. Console.WriteLine("The square root calculator needs a number greater than zero.\n");
  19. Console.WriteLine("Please enter a number to find the square root of and then press \'Enter\': ");
  20. }
  21. else if (x < 0)
  22. {
  23. Console.WriteLine("The square root calculator needs a number greater than zero.\n");
  24. Console.WriteLine("Please enter a number to find the square root of and then press \'Enter\': ");
  25. }
  26. else
  27. {
  28. detector = true;
  29. userInput = Convert.ToDouble(x);
  30. }
  31. }
  32.  
  33. double y = userInput * (userInput * .1);
  34. double answer;
  35. for (int i = 0; i < 100; i++)
  36. {
  37. answer = 0.5 * (y + (userInput / y));
  38. // takes output and causes the next iteration of the formula to use the output to calculate the new value
  39. y = answer;
  40. }
  41. Console.WriteLine("\nYour answer is: " + "{0:0.#######}", y);
  42. Console.ReadKey();
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement