diff --git a/EnthaeltZiffer/EnthaeltZiffer.cs b/EnthaeltZiffer/EnthaeltZiffer.cs new file mode 100644 index 0000000000000000000000000000000000000000..79f8d859c7d94f8a50b3120d2402362a91264f7f --- /dev/null +++ b/EnthaeltZiffer/EnthaeltZiffer.cs @@ -0,0 +1,39 @@ +class Program +{ + // Prüft, ob zahl ziffern enthält + // Bei genau1 wird nur dann true zurückgeliefert, + // wenn die ziffer genau 1x enthalten + static bool Enthaelt(int zahl, int ziffer, bool genau1=false) + { + int anzahl = 0; + + while (zahl > 0) + { + int letzteZiffer = zahl % 10; + if (letzteZiffer == ziffer) + anzahl++; + zahl /= 10; + } + + if (genau1) + return anzahl == 1; + else + return anzahl > 0; + } + + // Hauptprogramm + static void Main(string[] args) + { + Console.Write("Untere Grenze eingeben: "); + int a = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Obere Grenze eingeben: "); + int b = Convert.ToInt32(Console.ReadLine()); + + for (int zahl = a; zahl <= b; zahl++) + { + if (Enthaelt(zahl, 7, true)) + Console.WriteLine(zahl); + } + } +} diff --git a/EnthaeltZiffer/EnthaeltZiffer.csproj b/EnthaeltZiffer/EnthaeltZiffer.csproj new file mode 100644 index 0000000000000000000000000000000000000000..40c60dd4c884340c455eab8a0020f7c681a4e76c --- /dev/null +++ b/EnthaeltZiffer/EnthaeltZiffer.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Exchange/Exchange.cs b/Exchange/Exchange.cs new file mode 100644 index 0000000000000000000000000000000000000000..c67e47e73e5324476b94823d75901fe13baec3f9 --- /dev/null +++ b/Exchange/Exchange.cs @@ -0,0 +1,36 @@ +class Program +{ + // Vertauscht die Werte der beiden Parameter + static void Exchange(ref double x, ref double y) + { + double tmp = x; + x = y; + y = tmp; + } + + static void Main() + { + // Hauptprogramm + double zahl1 = 17; + double zahl2 = 23; + + Console.WriteLine($"zahl1 = {zahl1}, zahl2 = {zahl2}"); + Exchange(ref zahl1, ref zahl2); + Console.WriteLine("Exchange!"); + Console.WriteLine($"zahl1 = {zahl1}, zahl2 = {zahl2}"); + + /* + Geht nicht: + + int zahl3 = 12; + int zahl4 = 34; + Exchange(ref zahl3, ref zahl4); + + Grund: Bei Call-by - Reference wird ja die Speicherreferenz übergeben + Damit das funktioniert muss die übergebene Variable vom gleichen Typ + wie der Parameter sein. + Man kann ja nicht einfach mit den Bits einer Integer-Zahl + den Speicher einer Double-Variablen überschreiben. + */ + } +} \ No newline at end of file diff --git a/Exchange/Exchange.csproj b/Exchange/Exchange.csproj new file mode 100644 index 0000000000000000000000000000000000000000..40c60dd4c884340c455eab8a0020f7c681a4e76c --- /dev/null +++ b/Exchange/Exchange.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/PiMonteCarlo/PiMonteCarlo.cs b/PiMonteCarlo/PiMonteCarlo.cs new file mode 100644 index 0000000000000000000000000000000000000000..6f2959858d7a8c9c6e3af12f579b984453d111d9 --- /dev/null +++ b/PiMonteCarlo/PiMonteCarlo.cs @@ -0,0 +1,30 @@ +class Program +{ + static double Pi(int n = 1000000) + { + Random zufall = new Random(); // Anlegen des Zufallsgenerators; einmalig! + + int drin = 0; // Anzahl der Treffer im Einheitskreis + for (int i = 0; i < n; i++) + { + // Bestimmung der x und y Koordinate + double x = zufall.NextDouble(); + double y = zufall.NextDouble(); + + // Pythagoras-Test: Hypothenuse aus x^2 und y^2 kürzer als 1? + if ((x * x + y * y) <= 1) + drin++; + } + + // Pi = 4 * Treffer/alle Versuche + return 4.0 * drin / n; + } + + // Hauptprogramm + static void Main() + { + Console.WriteLine("Berechnung der Zahl PI:"); + double pi = Pi(100000); + Console.WriteLine($"Pi: {pi} - {Math.PI} = {pi - Math.PI}"); + } +} diff --git a/PiMonteCarlo/PiMonteCarlo.csproj b/PiMonteCarlo/PiMonteCarlo.csproj new file mode 100644 index 0000000000000000000000000000000000000000..40c60dd4c884340c455eab8a0020f7c681a4e76c --- /dev/null +++ b/PiMonteCarlo/PiMonteCarlo.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/PotenzRekursiv/PotenzRekursiv.cs b/PotenzRekursiv/PotenzRekursiv.cs new file mode 100644 index 0000000000000000000000000000000000000000..fff4a6e5c3f473e8c98006d088bfb08fd4705601 --- /dev/null +++ b/PotenzRekursiv/PotenzRekursiv.cs @@ -0,0 +1,28 @@ +// Iterativ +static double Potenz(double x, int n) +{ + double potenz = 1; + + for (int i = 1; i <= n; i++) + potenz *= x; + + return potenz; +} + +// Rekursiv +static double PotenzRek(double x, int n) +{ + if (n == 0) + return 1; + else + return x * PotenzRek(x, n - 1); +} + +// Hauptprogramm zum Test: +Console.Write("Basis eingeben: "); +double basis = Convert.ToDouble(Console.ReadLine()); +Console.Write("Exponent eingeben: "); +int exponent = Convert.ToInt32(Console.ReadLine()); + +Console.WriteLine($"{basis} hoch {exponent}: {PotenzRek(basis, exponent)}"); + diff --git a/PotenzRekursiv/PotenzRekursiv.csproj b/PotenzRekursiv/PotenzRekursiv.csproj new file mode 100644 index 0000000000000000000000000000000000000000..40c60dd4c884340c455eab8a0020f7c681a4e76c --- /dev/null +++ b/PotenzRekursiv/PotenzRekursiv.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Uebung06.sln b/Uebung06.sln new file mode 100644 index 0000000000000000000000000000000000000000..cdbca1fb645cee8ed9e7e894a706327f1898bafd --- /dev/null +++ b/Uebung06.sln @@ -0,0 +1,55 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EnthaeltZiffer", "EnthaeltZiffer\EnthaeltZiffer.csproj", "{BFC9A5F0-EC5C-4E26-AA5D-351632C2D56C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PotenzRekursiv", "PotenzRekursiv\PotenzRekursiv.csproj", "{74A19260-1005-4144-9F72-7CCD6B91F203}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exchange", "Exchange\Exchange.csproj", "{9C603C35-CF22-4BE7-8826-F72BAD75E94E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiMonteCarlo", "PiMonteCarlo\PiMonteCarlo.csproj", "{83878293-F2D5-4391-A6FE-3E993031EECE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZeichneDreieck", "ZeichneDreieck\ZeichneDreieck.csproj", "{10A78230-219C-4836-9597-CB3BE2D8BFDC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WurzelHeron", "WurzelHeron\WurzelHeron.csproj", "{66111554-B6B2-4A76-8CBE-FAAE00858290}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BFC9A5F0-EC5C-4E26-AA5D-351632C2D56C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BFC9A5F0-EC5C-4E26-AA5D-351632C2D56C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BFC9A5F0-EC5C-4E26-AA5D-351632C2D56C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BFC9A5F0-EC5C-4E26-AA5D-351632C2D56C}.Release|Any CPU.Build.0 = Release|Any CPU + {74A19260-1005-4144-9F72-7CCD6B91F203}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {74A19260-1005-4144-9F72-7CCD6B91F203}.Debug|Any CPU.Build.0 = Debug|Any CPU + {74A19260-1005-4144-9F72-7CCD6B91F203}.Release|Any CPU.ActiveCfg = Release|Any CPU + {74A19260-1005-4144-9F72-7CCD6B91F203}.Release|Any CPU.Build.0 = Release|Any CPU + {9C603C35-CF22-4BE7-8826-F72BAD75E94E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9C603C35-CF22-4BE7-8826-F72BAD75E94E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9C603C35-CF22-4BE7-8826-F72BAD75E94E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9C603C35-CF22-4BE7-8826-F72BAD75E94E}.Release|Any CPU.Build.0 = Release|Any CPU + {83878293-F2D5-4391-A6FE-3E993031EECE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {83878293-F2D5-4391-A6FE-3E993031EECE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {83878293-F2D5-4391-A6FE-3E993031EECE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {83878293-F2D5-4391-A6FE-3E993031EECE}.Release|Any CPU.Build.0 = Release|Any CPU + {10A78230-219C-4836-9597-CB3BE2D8BFDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10A78230-219C-4836-9597-CB3BE2D8BFDC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10A78230-219C-4836-9597-CB3BE2D8BFDC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10A78230-219C-4836-9597-CB3BE2D8BFDC}.Release|Any CPU.Build.0 = Release|Any CPU + {66111554-B6B2-4A76-8CBE-FAAE00858290}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {66111554-B6B2-4A76-8CBE-FAAE00858290}.Debug|Any CPU.Build.0 = Debug|Any CPU + {66111554-B6B2-4A76-8CBE-FAAE00858290}.Release|Any CPU.ActiveCfg = Release|Any CPU + {66111554-B6B2-4A76-8CBE-FAAE00858290}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F8BD3E6E-9C6F-41DF-B3D7-EC608E0687C5} + EndGlobalSection +EndGlobal diff --git a/WurzelHeron/WurzelHeron.cs b/WurzelHeron/WurzelHeron.cs new file mode 100644 index 0000000000000000000000000000000000000000..a8a8e92ced6a3cc702ccca0da635ded9e04fcc61 --- /dev/null +++ b/WurzelHeron/WurzelHeron.cs @@ -0,0 +1,25 @@ +class Heron +{ + // Funktion zur Berechnung der Wurzel (Square Root) nach Heron + static double Sqrt(double x) + { + // Startwert + double wurzel = x / 2; + + // Heron-Formel + while (Math.Abs(x - wurzel * wurzel) > 1e-12) + wurzel = 0.5 * (wurzel + x / wurzel); + + return wurzel; + } + + static void Main(string[] args) + { + Console.WriteLine("Wurzelberechnung mit dem Heronverfahren:"); + for (double d = 0; d <= 100; d += 10) + { + Console.WriteLine($"{d,4} | {Sqrt(d),8:F5} | {Math.Sqrt(d),8:F5} | {Math.Sqrt(d) - Sqrt(d),12}"); + } + Console.WriteLine(); + } +} diff --git a/WurzelHeron/WurzelHeron.csproj b/WurzelHeron/WurzelHeron.csproj new file mode 100644 index 0000000000000000000000000000000000000000..206b89a9a8b9320db4b017a262b565f104489193 --- /dev/null +++ b/WurzelHeron/WurzelHeron.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/ZeichneDreieck/ZeichneDreieck.cs b/ZeichneDreieck/ZeichneDreieck.cs new file mode 100644 index 0000000000000000000000000000000000000000..80d5044c68a90ca0b87f848ec6ac38242bdf2da0 --- /dev/null +++ b/ZeichneDreieck/ZeichneDreieck.cs @@ -0,0 +1,41 @@ +class Program +{ + static void ZeichneDreieck(int n, char c = '*', bool zentriert = false) + { + // Man könnte jetzt die beiden Code-Blöcke auch in einen zusammenfassen, + // weil die Schleifen ja im Prinzip das Gleiche machen. + // Versuchen Sie mal, den Code umzustellen! + if (!zentriert) + { + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= i; j++) + Console.Write(c); + + Console.WriteLine(); + } + } + else // zentriert + { + for (int i = 1, leer = n - 1; i <= n; i++, leer--) + { + // Leerzeichen vorher einfügen + for (int j = 1; j <= leer; j++) + Console.Write(' '); + + for (int j = 1; j <= 2 * i - 1; j++) + Console.Write(c); + + Console.WriteLine(); + } + } + } + + // Hauptprogramm + static void Main() + { + ZeichneDreieck(6, '*'); + ZeichneDreieck(3); + ZeichneDreieck(5, '#', true); + } +} diff --git a/ZeichneDreieck/ZeichneDreieck.csproj b/ZeichneDreieck/ZeichneDreieck.csproj new file mode 100644 index 0000000000000000000000000000000000000000..206b89a9a8b9320db4b017a262b565f104489193 --- /dev/null +++ b/ZeichneDreieck/ZeichneDreieck.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project>