C# exercises with solutions
1. Even numbers Beginner
Print all even numbers from 1 to 10 – each on its own line.
Expected output: 2 ⏎ 4 ⏎ 6 ⏎ 8 ⏎ 10
Note
In the for loop, start at 2 and count with i += 2, or check with i % 2 == 0.
Show sample solution
for (int i = 2; i <= 10; i += 2)
{
Console.WriteLine(i);
}2. FizzBuzz (1–15) Beginner
Print the numbers 1 to 15. For multiples of 3 print Fizz instead, for multiples of 5 print Buzz, and for both print FizzBuzz.
Expected output: 1 ⏎ 2 ⏎ Fizz ⏎ 4 ⏎ Buzz ⏎ Fizz ⏎ 7 ⏎ 8 ⏎ Fizz ⏎ Buzz ⏎ 11 ⏎ Fizz ⏎ 13 ⏎ 14 ⏎ FizzBuzz
Note
Check the "divisible by 15" case first, then 3, then 5.
Show sample solution
for (int i = 1; i <= 15; i++)
{
if (i % 15 == 0) Console.WriteLine("FizzBuzz");
else if (i % 3 == 0) Console.WriteLine("Fizz");
else if (i % 5 == 0) Console.WriteLine("Buzz");
else Console.WriteLine(i);
}3. Reverse a word Beginner
Reverse the string "Hello" and print it.
Expected output: olleH
Note
wort.Reverse() (LINQ) returns the characters backwards – you then build a string again from them with new string(...ToArray()).
Show sample solution
string wort = "Hello";
Console.WriteLine(new string(wort.Reverse().ToArray()));4. Sum of squares Beginner
Calculate the sum of the squares of the numbers 1 to 10 and print it.
Expected output: 385
Note
Enumerable.Range(1, 10) generates the numbers, Sum(x => ...) adds up the squares.
Show sample solution
Console.WriteLine(Enumerable.Range(1, 10).Sum(x => x * x));5. Palindrome method Intermediate
Write a method IstPalindrom(string text) that returns true if the text reads the same forwards and backwards (ignore case). Print IstPalindrom("Anna") and IstPalindrom("Python").
Expected output: True ⏎ False
Note
First convert with ToLower() and compare the text with its reversed version.
Show sample solution
Console.WriteLine(IstPalindrom("Anna"));
Console.WriteLine(IstPalindrom("Python"));
static bool IstPalindrom(string text)
{
string t = text.ToLower();
return t == new string(t.Reverse().ToArray());
}6. Most frequent word Intermediate
Find the most frequent word in "a b a c b a" and print it.
Expected output: a
Note
Split(' '), then GroupBy(w => w), sort descending by g.Count() and take the first Key.
Show sample solution
string text = "a b a c b a";
string wort = text.Split(' ')
.GroupBy(w => w)
.OrderByDescending(g => g.Count())
.First()
.Key;
Console.WriteLine(wort);7. Primes up to 30 Intermediate
Print all prime numbers up to 30 on one line, separated by spaces.
Expected output: 2 3 5 7 11 13 17 19 23 29
Note
A number n is prime if it is not divisible by any number from 2 to n-1. Enumerable.Range(2, n - 2).All(...) checks that; string.Join(" ", ...) produces the line.
Show sample solution
var primes = Enumerable.Range(2, 29)
.Where(n => Enumerable.Range(2, n - 2).All(d => n % d != 0));
Console.WriteLine(string.Join(" ", primes));8. Areas with pattern matching Advanced
Define the records Quadrat(int Seite) and Rechteck(int Breite, int Hoehe) with a common interface IForm. Write a method Flaeche(IForm f) using a switch expression and print the areas of Quadrat(4) and Rechteck(3, 5).
Expected output: 16 ⏎ 15
Note
The records implement IForm. In the switch expression, Quadrat q => ... and Rechteck(var b, var h) => ... (positional pattern) match.
Show sample solution
Console.WriteLine(Flaeche(new Quadrat(4)));
Console.WriteLine(Flaeche(new Rechteck(3, 5)));
static int Flaeche(IForm f) => f switch
{
Quadrat q => q.Seite * q.Seite,
Rechteck(var b, var h) => b * h,
_ => throw new ArgumentException("Unknown shape")
};
interface IForm { }
record Quadrat(int Seite) : IForm;
record Rechteck(int Breite, int Hoehe) : IForm;