C# :: Aufgabe #213
4 Lösungen

Babylonisches Wurzelziehen / Heron-Verfahren / Heronsches Näherungsverfahren
Anfänger - C#
von Exception
- 03.06.2018 um 08:27 Uhr
Die zu schreibende Anwendung soll eine positive Zahl entgegennehmen und durch das Heron-Verfahren näherungsweise die Quadratwurzel berechnen.
Zusatz:
Es werden die einzelnen Schritte ausgegeben, siehe Beispiel.
Beispiel: mit Wurzel aus 12
~ Viel Spaß :)
Zusatz:
Es werden die einzelnen Schritte ausgegeben, siehe Beispiel.
Beispiel: mit Wurzel aus 12
...
Die Wurzel aus 12 ist zwischen 3.5 und 3.4
...
Die Wurzel aus 12 ist zwischen 3.45 und 3.46
...
~ Viel Spaß :)
Lösungen:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BabylonischesWurzelziehen { class Program { static void Main(string[] args) { Console.WriteLine("Wurzel angeben:"); double wurzel = Convert.ToDouble(Console.ReadLine()); Console.Clear(); Console.WriteLine("Schritteanzahl angeben:"); double schritte = Convert.ToInt32(Console.ReadLine()); Console.Clear(); double startwert = (wurzel + 1) / 2; double lastVal = startwert; for(int i = 0; i< schritte; i++) { lastVal = 0.5 * (lastVal + wurzel / lastVal); Console.WriteLine(lastVal); } Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Heron_Verfahren { class Program { static void Main(string[] args) { Console.Title = "Heron-Verfahren"; Console.Write("Bitte Zahl eingeben, von der Sie die Wurzel wollen.\t"); double res; while (!(double.TryParse(Console.ReadLine(), out res))) { Console.WriteLine("Eingabe ungültig. Bitte erneut versuchen!"); } decimal res1 = Convert.ToDecimal(res); decimal x1 = Convert.ToDecimal(res); decimal x2 = Convert.ToDecimal(1); for(int i = 0; i < 50; i++) { x1 = (x1 + x2) / 2; x2 = (res1 / x1); if (x1 == x2) { Console.WriteLine("Der Wert ist {0}", x1); i = 50; } else { Console.WriteLine("Der Wert liegt zwischen {0} und {1}", x1, x2); } } Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Heron_Verfahren { class Program { static void Main(string[] args) { int x = int.Parse(Console.ReadLine()); double y = (x + 1) / 2; for(int i = 1;i<=10;i++) { double result = 0; result = ((x / y) + y)*1/2; Console.WriteLine(result); y = result; } Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Heron_Verfahren { class Program { static void Main(string[] args) { int x = int.Parse(Console.ReadLine()); double y = (x + 1) / 2; for(int i = 1;i<=10;i++) { double result = 0; result = ((x / y) + y)*1/2; Console.WriteLine(result); y = result; } Console.ReadKey(); } } }