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

.

parent 1d529db3
No related branches found
No related tags found
No related merge requests found
// Funktion zur Berechnung eines Bezugspreises unter Berücksichtigung von
// Versandkosten und Rabatt
static double Bezugspreis(double listenPreis, int versandArt, double rabatt = 0)
{
// Variable für Return-Wert
double preis = listenPreis;
// Rabatt-Berechnung
if (rabatt < 0)
return -1; // Fehlerfall
preis -= preis * rabatt / 100.0;
// Versandkosten-Berechnung - hier mal mit switch-Expression
double porto = versandArt switch
{
1 => 4.90, // Päckchen
2 => 6.50, // Paket < 10kg
3 => 12.00, // Paket > 10kg
_ => -1 // Fehlerfall
};
if (porto < 0)
return -1; // Fehlerfall
preis += porto;
return preis;
}
Console.WriteLine($"Fall 1: Preis = {Bezugspreis(125, 2, 10):0.00} Euro");
Console.WriteLine($"Fall 2: Preis = {Bezugspreis(200, 1, 3):0.00} Euro");
Console.WriteLine($"Fall 3: Preis = {Bezugspreis(375, 3, 0):0.00} Euro");
Console.WriteLine($"Fehler: Preis = {Bezugspreis(50, 4, 0):0.00} Euro");
Console.WriteLine($"Fehler: Preis = {Bezugspreis(150, 1, -1):0.00} Euro");
Console.ReadKey();
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
namespace Uebung
{
class Program
{
// Potenzfunktion
static double Potenz(double x, int n)
{
double potenz = 1;
for (int i = 1; i <= n; i++)
potenz *= x;
return potenz;
}
// Hauptprogramm
static void Main()
{
Console.Write("Basis eingeben: ");
double basis = Convert.ToDouble(Console.ReadLine());
Console.Write("Exponent eingeben: ");
int exponent = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <= exponent; i++)
Console.WriteLine($"{basis}^{i}: {Potenz(basis, i),4}");
}
}
}
\ 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>
namespace Uebung
{
class Program
{
static int QuerSumme(int zahl)
{
int summe = 0;
while (zahl > 0)
{
summe += zahl % 10;
zahl /= 10;
}
return summe;
}
static int QuerProdukt(int zahl)
{
if (zahl == 0)
return 0;
int produkt = 1;
while (zahl > 0)
{
produkt *= zahl % 10;
zahl /= 10;
}
return produkt;
}
static bool IstQuerZahl(int zahl)
{
return QuerSumme(zahl) + QuerProdukt(zahl) == zahl;
}
// Hauptprogramm
static void Main()
{
for (int zahl = 2; zahl < 1000; zahl++)
{
// Console.WriteLine($"{zahl,5} {QuerSumme(zahl),5} {QuerProdukt(zahl),5}");
if (IstQuerZahl(zahl))
Console.WriteLine($"{zahl} ist eine Querzahl!");
}
}
}
}
\ 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>
static int Twist(int zahl)
{
int twist = 0;
while (zahl > 0)
{
int ziffer = zahl % 10;
twist = twist * 10 + ziffer;
zahl /= 10;
}
return twist;
}
// Hauptprogramm
int zahl = 4567;
Console.WriteLine($"{zahl} umgekehrt: {Twist(zahl)}");
zahl = 1234567890;
Console.WriteLine($"{zahl} umgekehrt: {Twist(zahl)}");
<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}") = "PotenzFunktion", "PotenzFunktion\PotenzFunktion.csproj", "{EC9A3624-8777-41A5-A708-E3765BE52E5A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QuerZahlen", "QuerZahlen\QuerZahlen.csproj", "{CC129094-DD5F-4AB8-B685-59F8A9E6694A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Twist", "Twist\Twist.csproj", "{2EB67C10-2D6D-4283-AD11-240C8CE010C3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bezugspreis", "Bezugspreis\Bezugspreis.csproj", "{92656707-21E5-48D2-9FB1-E43A584DDD9D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EC9A3624-8777-41A5-A708-E3765BE52E5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC9A3624-8777-41A5-A708-E3765BE52E5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC9A3624-8777-41A5-A708-E3765BE52E5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC9A3624-8777-41A5-A708-E3765BE52E5A}.Release|Any CPU.Build.0 = Release|Any CPU
{CC129094-DD5F-4AB8-B685-59F8A9E6694A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC129094-DD5F-4AB8-B685-59F8A9E6694A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC129094-DD5F-4AB8-B685-59F8A9E6694A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC129094-DD5F-4AB8-B685-59F8A9E6694A}.Release|Any CPU.Build.0 = Release|Any CPU
{2EB67C10-2D6D-4283-AD11-240C8CE010C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2EB67C10-2D6D-4283-AD11-240C8CE010C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2EB67C10-2D6D-4283-AD11-240C8CE010C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2EB67C10-2D6D-4283-AD11-240C8CE010C3}.Release|Any CPU.Build.0 = Release|Any CPU
{92656707-21E5-48D2-9FB1-E43A584DDD9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92656707-21E5-48D2-9FB1-E43A584DDD9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92656707-21E5-48D2-9FB1-E43A584DDD9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92656707-21E5-48D2-9FB1-E43A584DDD9D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3F003FC-52A5-4A88-9F70-94B7949BBBFC}
EndGlobalSection
EndGlobal
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();
}
}
}
static void Main()
{
ZeichneDreieck(6, '$');
Console.WriteLine();
ZeichneDreieck(3);
Console.WriteLine();
ZeichneDreieck(5, '#', true);
}
}
\ 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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment