Ask for Two Numbers and Show Their Sum in C#
Table of Contents
- Ask for Two Numbers and Show Their Sum in C#
- Introduction
- Objectives
- Step-by-Step Instructions with Beginner-Friendly Code
- Step 1: Prepare Your Folder
- Step 2: Open VS Code
- Step 3: Open Terminal
- Step 4: Create a New Console Project
- Step 5: Navigate to the Project Folder
- Step 6: Write the Code
- Step 7: Run the Program
- Expected Output
- Code Explanation
- Summary
- Quiz (Bloom’s Taxonomy)
- Exercises, Assessment, and Lab Exam
- Introduction to Exercises
- Exercises
- Assessment
- Lab Exam
Introduction
In programming, one of the most common and essential tasks is working with user input and performing calculations based on that input. Real-world applications, like calculators, point-of-sale systems, and grading systems, all depend on reading data from users, processing it, and displaying results.
Up until now, we have been working mostly with fixed values that are already in the program. However, real applications need to accept dynamic data — information that changes based on what the user provides. This is where input and output operations come in.
In C#, Console.ReadLine()
allows us to capture user input from the keyboard. By default, anything typed is treated as text (string). But what if you need to perform mathematical operations like addition, subtraction, multiplication, or division? In that case, you must convert the text into numbers using type conversion, such as Convert.ToInt32()
for whole numbers.
In this lesson, we will create a program that asks the user to enter two numbers, calculates their sum, and displays the result. This simple task will help you practice reading inputs, converting data types, and outputting meaningful information.
Mastering this concept is a critical step toward building more complex programs like budgeting tools, inventory management systems, or even games.
Objectives
Introduction to Objectives:
The purpose of this lesson is to guide you through a learning process where you first understand the concepts, then learn by example, practice by coding, and finally apply your skills to real-world scenarios. These four stages are part of the outcomes-based education (OBE) framework designed to ensure you gain lasting knowledge.
By the end of this lesson, you should be able to:
- Understand – Explain how user input and type conversion work in C#, specifically using
Console.ReadLine()
andConvert.ToInt32()
to handle numbers. - Learn – Identify and use the correct syntax for reading two numbers and calculating their sum.
- Practice – Build and run a C# console application that asks for two numbers, processes them, and displays the result clearly.
- Apply – Modify the program to perform other basic math operations such as subtraction, multiplication, and division.
Step-by-Step Instructions with Beginner-Friendly Code
Follow these steps carefully to build the program. Make sure you complete each one before moving on.
Step 1: Prepare Your Folder
- Open Drive D: on your computer.
- Create a folder for this course using this format:
2025-1_lastname_bsis_1a
Example:
2025-1_doe_bsis_1a
- Inside this folder, create a subfolder for each lesson or activity.
Step 2: Open VS Code
- Launch Visual Studio Code (VS Code).
- Go to File → Open Folder and select your course folder.
Step 3: Open Terminal
- In VS Code, click Terminal → New Terminal.
- The terminal will open at the bottom of the window.
Step 4: Create a New Console Project
In the terminal, type:
dotnet new console -n sum_two_numbers
This command will create a folder called sum_two_numbers
containing:
sum_two_numbers.csproj
– The project configuration file.Program.cs
– The file where you will write your C# code.
cd sum_two_numbers
Step 6: Write the Code
- Open
Program.cs
in VS Code. - Replace the default content with the following code:
using System;
namespace SumTwoNumbersApp
{
class Program
{
static void Main(string[] args)
{
// Ask the user for the first number
Console.Write("Enter the first number: ");
int firstNumber = Convert.ToInt32(Console.ReadLine());
// Ask the user for the second number
Console.Write("Enter the second number: ");
int secondNumber = Convert.ToInt32(Console.ReadLine());
// Calculate the sum
int sum = firstNumber + secondNumber;
// Display the result
Console.WriteLine("The sum of " + firstNumber + " and " + secondNumber + " is: " + sum);
}
}
}
Step 7: Run the Program
In the terminal, type:
dotnet run
Expected Output
Enter the first number: 8
Enter the second number: 12
The sum of 8 and 12 is: 20
Code Explanation
using System;
- This imports the System namespace, giving us access to basic input and output commands like
Console.WriteLine()
andConsole.ReadLine()
.
- This imports the System namespace, giving us access to basic input and output commands like
- Variables
int firstNumber
andint secondNumber
are variables used to store the user’s numbers.int sum
stores the result of adding the two numbers.
Console.Write
vsConsole.WriteLine
Console.Write
displays a message but keeps the cursor on the same line for user input.Console.WriteLine
moves the cursor to the next line after displaying a message.
- Type Conversion:
Convert.ToInt32()
Console.ReadLine()
reads user input as text (string).Convert.ToInt32()
converts this text into an integer so we can perform math operations.
- Final Output
- The program then displays a clear message showing the two numbers and their sum.
Summary
In this lesson, you learned how to create an interactive program that:
- Asks the user for two numbers.
- Converts the input from text to integers using
Convert.ToInt32()
. - Performs an addition operation.
- Displays the result to the screen.
This simple activity introduces you to input handling, type conversion, and basic arithmetic operations in C#. These skills are essential for developing more advanced programs where user interaction and calculations are necessary, such as calculators, order systems, or academic grading tools.
Quiz (Bloom’s Taxonomy)
- Which method is used to read user input in C#?
A. Console.Input()
B. Console.ReadLine()
C. Console.Get()
D. Console.WriteLine() - What does
Convert.ToInt32()
do in this program?
A. Converts integers to text
B. Converts text to integers
C. Displays numbers on the screen
D. Adds two numbers together - If the user enters 5 and 7, what will be displayed?
A. The sum of 5 and 7 is: 12
B. The sum of 5 and 7 is: 57
C. The sum of 5 and 7 is: 5+7
D. The sum is undefined - Why is type conversion necessary in this program?
A. To make the program shorter
B. Because Console.ReadLine() returns data as text
C. To display text instead of numbers
D. It is optional in all cases - What happens if the user enters non-numeric data like “Hello”?
A. The program automatically converts it to 0
B. It displays “Hello” on the screen
C. The program throws an error
D. It skips that input
Exercises, Assessment, and Lab Exam
Introduction to Exercises
The following activities will help you strengthen your understanding of input, type conversion, and arithmetic operations in C#. These tasks start simple and become slightly more challenging to prepare you for real-world programming scenarios.
Exercises
- Modify the program to display the difference between two numbers instead of the sum.
- Update the code to calculate both the sum and product of two numbers.
- Ask for three numbers and display their total sum.
- Create a program that asks for two numbers and displays which one is greater.
- Add error handling to display a message if the user enters non-numeric data.
Assessment
- Written Quiz:
Answer the 5 multiple-choice questions provided above. - Performance Task:
Build a program that:- Asks the user for three numbers.
- Displays the sum, average, and product of those numbers.
Lab Exam
Write a C# program that:
- Asks the user for two numbers.
- Displays all four basic math operations:
- Sum
- Difference
- Product
- Quotient (result of division)
Expected Output Example:
Enter the first number: 10
Enter the second number: 5
Sum: 15
Difference: 5
Product: 50
Quotient: 2
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.