In this C# tutorial, I have explained how to write a program to add, subtract, multiply, and divide two numbers in C#.
To write a program in C# that performs addition, subtraction, multiplication, and division on two numbers, start by reading the user’s input for the two numbers and the desired operation. Then, use a switch statement to perform the corresponding arithmetic operation based on the user’s choice. For clarity and ease of maintenance, each operation – add, subtract, multiply, divide – can be implemented in separate methods. This approach ensures a clean, user-friendly, and efficient program in C#.
Write A Program To Add, Subtract, Multiply, And Divide Two Numbers In C#
Now, let us write the complete program to add, subtract, multiply, and divide two numbers in C#.
Our program will prompt the user to enter two numbers and select an operation (addition, subtraction, multiplication, or division). The program will perform the corresponding operation and display the result based on the user’s choice.
Here’s the complete C# code:
using System;
namespace ArithmeticOperations
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the first number:");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the second number:");
double num2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Choose an operation: +, -, *, /");
string operation = Console.ReadLine();
double result = 0;
switch (operation)
{
case "+":
result = Add(num1, num2);
break;
case "-":
result = Subtract(num1, num2);
break;
case "*":
result = Multiply(num1, num2);
break;
case "/":
result = Divide(num1, num2);
break;
default:
Console.WriteLine("Invalid operation.");
return;
}
Console.WriteLine($"The result is: {result}");
}
static double Add(double a, double b)
{
return a + b;
}
static double Subtract(double a, double b)
{
return a - b;
}
static double Multiply(double a, double b)
{
return a * b;
}
static double Divide(double a, double b)
{
if (b != 0)
return a / b;
else
{
Console.WriteLine("Division by zero is not allowed.");
return 0;
}
}
}
}
Here you can see I ran the code using Visual Studio C# console application, and you can see the complete output in the screenshot below:
Here is the complete code explanation:
- Namespace Declaration: We start by declaring a namespace ArithmeticOperations. This is a container for classes and other namespaces.
- Class Definition: Our class Program encapsulates the main functionality of our application.
- Main Method: The Main method is the entry point of a C# application. Inside this method, we perform our operations.
- Reading Input: We use Console.ReadLine() to read input from the user.
- Performing Operations: The switch statement is used to select the operation based on the user’s input. We have four methods – Add, Subtract, Multiply, and Divide – each performing the respective operation.
- Error Handling: In the Divide method, we handle division by zero, which is an illegal operation in mathematics.
Conclusion
In this tutorial, I have learned how to create a simple C# program to add, subtract, multiply, and divide two numbers. I hope you learn how to write a program to add, subtract, multiply, and divide two numbers in C#.
You may also like:
- Write A Program To Find Numbers Above And Below The Average In C#
- How to Swap Two Numbers in C#?
- Write A Program To Find Factorial Of A Number In C#
- Write A C# Program To Add Two Numbers
- Write a Program to Check Armstrong Number in C#
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…