C# :: Aufgabe #99
12 Lösungen

Zahlen von 1 bis 100 aufaddieren
Anfänger - C#
von Torbo
- 15.05.2015 um 14:38 Uhr

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Gauß { class Program { static void Main(string[] args) { int a = 100; for (int i = 0; i < 1; i++) { a = (a + 1)*a / 2; } Console.WriteLine(a); Console.ReadLine(); } } }
Lösungen:

static void Main() { int zahl = 0; for (int i = 1; i <= 100; i++) zahl += i; Console.WriteLine(zahl); Console.ReadKey(true); }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _1_100_addieren { class Program { static void Main(string[] args) { int ergebnis = 0; for (int z1 = 0; z1 <= 100; z1++) { ergebnis = ergebnis + z1; } Console.WriteLine(ergebnis); Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Gauß { class Program { static void Main(string[] args) { int a = 100; for (int i = 0; i < 1; i++) { a = (a + 1)*a / 2; } Console.WriteLine(a); Console.ReadLine(); } } }

static void Main(string[] args) { for (int i = 0, Result = 0; i < 100; i++, Console.WriteLine("{0} + {1} = {2}", Result, i, Result += i)); Console.ReadKey(); }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Onetimepad { class Program { static void Main(string[] args) { string input = Console.ReadLine(); int inputInt = Int32.Parse(input); Console.WriteLine((((Int32)Math.Pow(inputInt, 2)) + inputInt) / 2); Console.ReadLine(); } } }

using System; using System.Linq; namespace IhateWinter { internal class Program { private static void Main(string[] args) { Console.WriteLine(Enumerable.Range(1, 100).Sum()); Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TYP_Addition { class Program { static void Main(string[] args) { int zahl = 0; for(int i = 1; i <= 100; i++) { zahl += i; } Console.WriteLine(zahl); Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Zahlung_Aufaddieren { class Program { static void Main(string[] args) { Console.WriteLine("Bitte wählen Sie die gewünschte Anzahl Zahlen, die Sie addieren möchten:"); string input = Console.ReadLine(); try { int max = Int32.Parse(input); Console.Clear(); int merker = 0; int ergebnis = 0; for (int i = 1; i <= max; i++) { ergebnis = i + merker; merker = ergebnis; Console.WriteLine(i + " : " + ergebnis); } Console.WriteLine("Das Ergebnis lautet: " + ergebnis); } catch (FormatException e) { Console.WriteLine("Falsche Eingabe, es folgt der Fehlertext aus dem System:"); Console.WriteLine(e.Message); } Console.ReadLine(); } } }

using System; namespace train_your_programmer_aufaddieren { class Program { static void Main(string[] args) { int Erg = 0; for (int i = 100; i >= 0; i--) { Erg = Erg + i; } Console.WriteLine(Erg); Console.Read(); } } }

using System; namespace Rextester { public class Program { public static int berechnung() { int ausgabe = 0; for(int i = 1; i <= 100; i++) { ausgabe += i; } return ausgabe; } public static void Main(string[] args) { Console.Write(berechnung()); } } }

static void Main(string[] args) { int a = 0; while(a != 100) { a += 1; Console.WriteLine(a); } Console.ReadLine(); }
NET 5.x; C# 9.x
Für die Gaußsche Summenformel wäre jetzt nicht unbedingt eine Schleife notwendig gewesen.
Lässt sich auch berechnen:
C#-Code
Für die Gaußsche Summenformel wäre jetzt nicht unbedingt eine Schleife notwendig gewesen.
Lässt sich auch berechnen:

static int Gauss(int n) => n * (n + 1) / 2;