Skip to main content
ANVISoftware Solutions

Console Calculator

Beginner2-4 hours

Your first program that does something useful. Reads input, validates it, calculates, and handles the cases that go wrong.

Technologies

The brief

What the finished thing needs to do.

  • Ask the user for two numbers and an operation: add, subtract, multiply or divide
  • Display the result
  • Reject input that is not a number, with a clear message
  • Refuse division by zero rather than crashing
  • Let the user perform another calculation without restarting the program

How to structure it

One file is fine for this project. The structure that matters is inside it.

Separate the three responsibilities: reading and validating input, performing the calculation, and displaying the result. Even at this size, a method per responsibility makes the program easier to extend.

The calculation method should take numbers and return a number. It should not read from the console or print anything — that separation is what makes it testable later.

Why these technologies

C# console application
No UI or web concerns to distract from the logic itself.
TryParse for input
Invalid input from a human is expected, not exceptional, so a check is more appropriate than exception handling.

Build it in this order

Each stage produces something that works. That matters — a project that only runs at the very end is a project people abandon.

  1. Add two fixed numbers

    Hard-code both values and print the sum. Confirm the project builds and runs.

  2. Read the numbers from input

    Use Console.ReadLine and convert. Do not worry about invalid input yet.

  3. Support all four operations

    Read an operator and branch. A switch expression reads better than a chain of ifs here.

  4. Validate the input

    Switch to TryParse. Print a specific message for non-numeric input.

  5. Handle division by zero

    Check before dividing rather than catching the exception afterwards.

  6. Loop until the user quits

    Wrap it in a loop with a clear exit condition.

  7. Extract methods

    Move the calculation into its own method that takes values and returns a result, with no console access.

Done means

How to know it is finished

Check each of these before moving on. If one fails, the project is not done yet — and that is useful information rather than a setback.

  • Entering text where a number is expected produces a helpful message and does not crash
  • Dividing by zero produces a message rather than an exception
  • The user can perform several calculations in one run and exit deliberately
  • The calculation logic is in a method that neither reads nor writes to the console

If you want to go further

Extensions worth attempting

Only once the core build meets every criterion above.

  • Support decimal input and think about which numeric type is appropriate
  • Add a power operation and a square root
  • Keep a history of calculations in the session and print it on request
  • Write unit tests for the calculation method — easy now that it is separated

Have a project worth talking through?

Tell us what you're building or what's slowing your current system down. We'll give you a direct read on scope and approach.