Skip to content
Snippets Groups Projects
Commit 128bc7e6 authored by Jens Albrecht's avatar Jens Albrecht
Browse files

.

parent 1888f679
No related branches found
No related tags found
No related merge requests found
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);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
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
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
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}");
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
// 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)}");
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

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
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();
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
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);
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment