Start learning free

LESSON 49 OF 347 · CALCULATIONS - CHALLENGES

Your First Challenge in C#: Sum of Two Numbers

Practice Read the full explanation

In this lesson we will introduce you to how challenges work and how to make the most out of them. Let's check your first coding challenge:


Sum of Two Numbers

You are given two numbers a and b and you should return their sum.

Examples:

Input: a = 4, b = 9

Output: 13

Explanation:
The sum of a and b is 4 + 9, which is 13.

This is the challenge statement. It explains what the challenge asks us to do and provides some examples to make sure we understand the statement.

Each challenge gives us some input and asks us to write some code that succesfully takes that input and uses it to produce some desired output.

In this case, the input is two numbers a and b and the output is their sum.


The class:

If you look at the code editor, you will see that everything is wrapped up inside a class:

class Solution {

}

You don't need to know anything about classes at this point, we will study them in future lessons. Just leave that code untouched.


The method:

Inside this class, we will always be provided with a method. Inside this method is where we should write our solution.

In our case the method is:

public int sum(int a, int b) {
    // Your solution goes here!
}

A method is just a function inside a class. When defining methods, we use visibility keywords like public in the method's header. Don't dwell on this, we will study methods in future lessons.


Parameters and return:

This method's parameters will always match the input described in the challange's statement. In our case, the parameters are a and b, the two numbers we are given.

The method should return a value which represents the desired output described in the challange's statement. In our case, it should return a + b.


Assignment:

Follow the Coding Tutorial and let's solve our first coding challenge: Sum of Two Numbers


STEP 1 OF 1
    Ctrl + Enter to check

    C# · CALCULATIONS - CHALLENGES

    Your First Challenge in C#, explained

    Problem overview

    This lesson is less about the sum itself and more about how challenges work. Every challenge gives you some input and asks for code that turns it into the right output. Here the input is two numbers, a and b, and the output is their sum. For a = 4 and b = 9 the answer is 13, because 4 + 9 = 13.

    How a challenge is laid out

    The editor always gives you the same skeleton:

    • The class. Everything sits inside class Solution { ... }. In C#, code lives inside classes. You don't need to know more than that yet. Leave the class as it is.
    • The method. Inside the class there is one method, here public int Sum(int a, int b). A method is a function that belongs to a class. This is where your solution goes.
    • The parameters. int a, int b are the inputs from the statement. Their names match the names in the statement, and each has the type int, a whole number.
    • The return type. The int before the method name says the method gives back a whole number. Your job is to return the value the statement calls the output.

    The word public says the method can be called from outside the class, which the lesson's tests need to do. The method name starts with a capital letter, Sum, because C# methods conventionally use PascalCase.

    Approach

    There is nothing to search for or loop over. You need the sum of the two inputs, and C# has the + operator for that. The only thing to remember is that a challenge method must return its answer, not print it. The tests call Sum(4, 9) and looks at the value that comes back.

    Step by step

    1. Find the comment // Write the first step below: inside the method body.
    2. Add the two parameters with a + b.
    3. Put return in front of that expression, so the result goes back to whoever called Sum, and end the line with a semicolon.

    The solution in C#

    class Solution {
        public int Sum(int a, int b) {
            // Write the first step below:
            return a + b;
        }
    }
    

    Some C# details in these few lines:

    • The statement ends with ;. Forgetting it is the most common first compile error.
    • If the method has a non-void return type like int, every path must end with a return of that type. An empty body fails to compile for this reason, which is why the starter code doesn't compile until you add your line.
    • Adding two int values gives an int, so the result matches the declared return type with no conversion.

    Edge cases and common mistakes

    • Printing instead of returning. Console.WriteLine(a + b); shows 13 on screen but returns nothing, so the method won't compile with the int return type and the tests get no value.
    • Overflow. An int holds values up to 2,147,483,647. By default C# does not report overflow. Sum(2147483647, 1) silently wraps around to -2147483648. The lesson's tests use small numbers, but it's worth knowing that int arithmetic has limits.
    • Negative numbers. They work with no special handling: Sum(-4, 9) returns 5.
    • Changing the signature. Renaming the method, or changing its parameter types or return type, stops the tests from calling it.

    Testing

    Try a few inputs in the editor above. Sum(4, 9) should return 13, Sum(0, 0) should return 0, and Sum(-4, 9) should return 5.

    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free