Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Subtract Two Numbers
In this article, we will learn how to subtract two numbers in C# using different programming approaches. Subtraction is one of the fundamental arithmetic operations where we find the difference between two values.
Problem Description
Given two numbers, we need to find the result of subtracting one number from another. We'll explore multiple methods to perform this operation in C#.
Example 1
-
Input:
number1 = 8
number2 = 12 - Output: 4
Explanation
The subtraction of number2 - number1, i.e., 12 - 8, will result in 4.
Example 2
-
Input:
number1 = 10
number2 = 10 - Output: 0
Explanation
The subtraction of number2 - number1, i.e., 10 - 10, will result in 0 as both numbers are equal.
Syntax
The basic syntax for subtraction in C# uses the minus (-) operator
result = number1 - number2;
Using Direct Subtraction
This is the simplest approach where we directly subtract one number from another using the subtraction operator
using System;
class Program {
static void Main(string[] args) {
int number1 = 15, number2 = 7;
int result = number1 - number2;
Console.WriteLine("The result of subtraction is: " + result);
// Example with different data types
double num1 = 25.5, num2 = 10.3;
double doubleResult = num1 - num2;
Console.WriteLine("Double subtraction result: " + doubleResult);
}
}
The output of the above code is
The result of subtraction is: 8 Double subtraction result: 15.2
Using Functions
This approach uses a dedicated function to perform subtraction, making the code more modular and reusable
using System;
class Program {
// Function to subtract two integers
static int SubtractNumbers(int a, int b) {
return a - b;
}
// Overloaded function for double values
static double SubtractNumbers(double a, double b) {
return a - b;
}
static void Main(string[] args) {
int number1 = 20, number2 = 8;
int result = SubtractNumbers(number1, number2);
Console.WriteLine("Integer subtraction: " + result);
double d1 = 15.75, d2 = 3.25;
double doubleResult = SubtractNumbers(d1, d2);
Console.WriteLine("Double subtraction: " + doubleResult);
}
}
The output of the above code is
Integer subtraction: 12 Double subtraction: 12.5
Using Class-Based Approach
This object-oriented approach encapsulates the subtraction logic within a class, providing better code organization and reusability
using System;
class Calculator {
// Method to perform subtraction
public int Subtract(int a, int b) {
return a - b;
}
// Method with validation
public double SubtractWithValidation(double a, double b) {
Console.WriteLine($"Subtracting {b} from {a}");
return a - b;
}
}
class Program {
static void Main(string[] args) {
Calculator calc = new Calculator();
int number1 = 25, number2 = 9;
int result = calc.Subtract(number1, number2);
Console.WriteLine("Result using class method: " + result);
double d1 = 18.5, d2 = 6.2;
double doubleResult = calc.SubtractWithValidation(d1, d2);
Console.WriteLine("Double result: " + doubleResult);
}
}
The output of the above code is
Result using class method: 16 Subtracting 6.2 from 18.5 Double result: 12.3
Comparison of Approaches
| Approach | Best For | Advantages |
|---|---|---|
| Direct Subtraction | Simple calculations | Quick, minimal code |
| Function-Based | Repeated operations | Reusable, organized |
| Class-Based | Complex applications | OOP principles, scalable |
Conclusion
C# provides multiple ways to subtract two numbers, from simple direct subtraction to object-oriented approaches. Choose the method based on your application's complexity and reusability requirements. All approaches have O(1) time and space complexity for the subtraction operation itself.
