C# :: Aufgabe #287 :: Lösung #1
3 Lösungen
#287
Die Collatz-Sequenz (3n+1)
Anfänger - C#
von DragStar
- 23.03.2020 um 09:34 Uhr
Schreibe ein Programm, welches die Eingabe einer Zahl anfordert und dann von dieser Zahl die Collatz-Sequenz ausgibt.
#1
von Waldgeist (2310 Punkte)
- 30.03.2020 um 10:28 Uhr
Hallo,
hier meine Version. Das Projekt findet Ihr als ZIP Datei im Anhang.
C#-Code
Hier der XAML:
C#-Code
hier meine Version. Das Projekt findet Ihr als ZIP Datei im Anhang.
using System;
using System.Windows;
namespace Collatz_Reihe
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void rechen_Click(object sender, RoutedEventArgs e)
{
{
ulong n = Convert.ToUInt64(eingabebox.Text);
string ausgabe = n.ToString() + " ";
while (n > 1)
{
if (n % 2 == 0)
{
n = n / 2;
ausgabe = ausgabe + n.ToString() + " ";
}
else
{
n = 3 * n + 1;
ausgabe = ausgabe + n.ToString() + " ";
}
}
ausgabebox.Text = ausgabe;
}
}
}
}
Hier der XAML:
<Window x:Class="Collatz_Reihe.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Collatz_Reihe"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="450">
<Grid>
<Label Content="Program zur Ermittlung der Collatz-Reihe." HorizontalAlignment="Left" Height="39" Margin="20,10,0,0" VerticalAlignment="Top" Width="751" />
<Label Content="Bitte geben Sie eine natürliche Zahl größer 0 ein (n>0)." HorizontalAlignment="Left" Height="55" Margin="20,54,0,0" VerticalAlignment="Top" Width="751" />
<TextBox x:Name="eingabebox" HorizontalAlignment="Left" Height="28" Margin="338,54,0,0" Text="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="83" TextAlignment="Left" />
<TextBox x:Name="ausgabebox" HorizontalAlignment="Left" Height="132" TextWrapping="Wrap" VerticalAlignment="Top" Width="398" Margin="23,236,0,0" />
<Label Content="Hier wird die Collatz-Reihe ausgegeben:" HorizontalAlignment="Left" Height="37" Margin="23,180,0,0" VerticalAlignment="Top" Width="398" />
<Button x:Name="rechen" Content="Berechne" HorizontalAlignment="Left" Height="60" Margin="23,109,0,0" VerticalAlignment="Top" Width="398" Click="rechen_Click" />
</Grid>
</Window>
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
