Thuta Learning
C#
ExercisesProgrammingBeginner

Practice Exercises: Fundamentals

C#Lesson 3240 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Practice Exercises: Fundamentals
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Practice Exercises: Fundamentals, in plain language

This exercise set is designed to help you revisit and practice the variables, operators, if/else, loops, and arrays concepts covered in the Basic chapter. There's no new teaching here — just a chance to actually write code using the syntax you've already learned. Treat each task as its own little project: create a new console app for it and run it. The tasks are simple, but writing and running the syntax hands-on will help it stick in your memory far better than just reading about it.

Problem → Why → Solution

The problem — why this concept matters

Passing one sample input does not complete an exercise or expose boundary bugs.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

Turn fundamentals exercises into small functions with input contracts, boundaries, invalid cases, and expected output rather than syntax-only questions. Predict before running and explain results with compiler or runtime evidence.

Mental model
  1. requirement
  2. reason and implement
  3. test and explain

Explain the core concept and runtime behavior of Practice Exercises: Fundamentals

Core syntax or pattern

csharp
// Task 1 starter
for (int i = 1; i <= 50; i++)
{
    if (i % 2 == 0)
    {
        Console.WriteLine(i);
    }
}

// Task 2 starter
int[] numbers = { 4, 8, 15, 16, 23, 42 };
int sum = 0;
foreach (int n in numbers)
{
    sum += n;
}
double average = (double)sum / numbers.Length;
Console.WriteLine($"Sum: {sum}, Average: {average}");

// Task 3 starter
Console.Write("Enter age: ");
int age = int.Parse(Console.ReadLine());
// use if / else if / else to classify Child, Teenager, or Adult

// Task 4 starter
Console.Write("Enter a sentence: ");
string text = Console.ReadLine();
int vowelCount = 0;
// use a loop to count the vowels

Turn fundamentals exercises into small functions with input contracts, boundaries, invalid cases, and expected output rather than syntax-only questions. Predict before running and explain results with compiler or runtime evidence.

Read the code step by step

  1. requirement// Task 1 starter

    This exercise set is designed to help you revisit and practice the variables, operators, if/else, loops, and arrays concepts covered in the Basic chapter. There's no new teaching here — just a chance to actually write code using the syntax you've already learned. Treat each task as its own little project: create a new console app for it and run it. The tasks are simple, but writing and running the syntax hands-on will help it stick in your memory far better than just reading about it.

  2. reason and implementsum += n;

    Turn fundamentals exercises into small functions with input contracts, boundaries, invalid cases, and expected output rather than syntax-only questions. Predict before running and explain results with compiler or runtime evidence.

  3. test and explain// use a loop to count the vowels

    Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.

Execution flow
  1. requirement
  2. reason and implement
  3. test and explain

Success

Explain the core concept and runtime behavior of Practice Exercises: Fundamentals

Failure

Passing one sample input does not complete an exercise or expose boundary bugs.

Let's think about this for a second

This exercise set is designed to help you revisit and practice the variables, operators, if/else, loops, and arrays concepts covered in the Basic chapter. There's no new teaching here — just a chance to actually write code using the syntax you've already learned. Treat each task as its own little project: create a new console app for it and run it. The tasks are simple, but writing and running the syntax hands-on will help it stick in your memory far better than just reading about it.

Exercises

(1) Print only the even numbers from 1 to 50 using a for loop. (2) Create an integer array (say, 6 elements) and use a foreach loop to find the sum and average of the numbers in the array. (3) Read a user-entered age with Console.ReadLine() and use if/else if/else to categorize it as "Child", "Teenager", or "Adult". (4) Read a string from the user and use a loop to count both the total number of characters and the number of vowels (a, e, i, o, u) in it.

Code Example

csharp
// Task 1 starter
for (int i = 1; i <= 50; i++)
{
    if (i % 2 == 0)
    {
        Console.WriteLine(i);
    }
}

// Task 2 starter
int[] numbers = { 4, 8, 15, 16, 23, 42 };
int sum = 0;
foreach (int n in numbers)
{
    sum += n;
}
double average = (double)sum / numbers.Length;
Console.WriteLine($"Sum: {sum}, Average: {average}");

// Task 3 starter
Console.Write("Enter age: ");
int age = int.Parse(Console.ReadLine());
// use if / else if / else to classify Child, Teenager, or Adult

// Task 4 starter
Console.Write("Enter a sentence: ");
string text = Console.ReadLine();
int vowelCount = 0;
// use a loop to count the vowels
You should see
All four tasks will print correct results to the console: the list of even numbers, the sum/average, the age category, and the vowel count.

5-Minute Challenge

Modify Task 2 so it also finds the largest and smallest numbers in the array — but without using Math.Max/Math.Min, using a loop instead (5 minutes).

A Quick Heads-Up

Remember that integer division (int / int) only returns an int result — don't forget to cast to (double) for calculations like averages or percentages.

Real-world usage

How to apply it in a real project

Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.

Mini challenge

Build it yourself

Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
// Task 1 starter
for (int i = 1; i <= 50; i++)
{
    if (i % 2 == 0)
    {
        Console.WriteLine(i);
    }
}

// Task 2 starter
int[] numbers = { 4, 8, 15, 16, 23, 42 };
int sum = 0;
foreach (int n in numbers)
{
    sum += n;
}
double average = (double)sum / numbers.Length;
Console.WriteLine($"Sum: {sum}, Average: {average}");

// Task 3 starter
Console.Write("Enter age: ");
int age = int.Parse(Console.ReadLine());
// use if / else if / else to classify Child, Teenager, or Adult

// Task 4 starter
Console.Write("Enter a sentence: ");
string text = Console.ReadLine();
int vowelCount = 0;
// use a loop to count the vowels

Expected: Explain the core concept and runtime behavior of Practice Exercises: Fundamentals

Show hint

Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.

Quick quiz 1 / 3

What best describes the core idea of Practice Exercises: Fundamentals?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • requirement → reason and implement → test and explain
  • Turn fundamentals exercises into small functions with input contracts, boundaries, invalid cases, and expected output rather than syntax-only questions. Predict before running and explain results with compiler or runtime evidence.
  • Passing one sample input does not complete an exercise or expose boundary bugs.
  • Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.

Unit testing C# in .NET — Microsoft Learn

Easy traps

  • Passing one sample input does not complete an exercise or expose boundary bugs.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.

You'll know it worked when: All four tasks will print correct results to the console: the list of even numbers, the sum/average, the age category, and the vowel count.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Practice Exercises: Fundamentals | Thuta Learning