C# Program to Subtract Two Numbers



Problem Description

In this problem, we are given two numbers, and we have to find the value obtained after subtracting one number from another. In this article, we are going to learn how we can subtract two numbers in C# using different approaches.

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, so they cancel each other and result in zero.

Approaches for Subtracting Two Numbers in C#

There are various approaches to subtracting two numbers, but here we will discuss three common and widely used methods:

  • Direct Subtraction
  • Using Functions
  • Using Class

Direct Subtraction Using Basic Arithmetic

This is the simple and direct approach for subtracting two numbers in any programming language. In this approach, we directly subtract the second number from the first number using the minus (-) operator.

Syntax

result = number1 - number2;

Implementation Code

using System;

class Program {
    static void Main(string[] args) {
        int number1 = 10, number2 = 4;
        int result = number1 - number2;
        Console.WriteLine("The result of subtraction is: " + result);
    }
}
    

Output

The result of subtraction is: 6

Time Complexity: O(1)
Space Complexity: O(1)

Using Functions

In this approach, we use a function that performs subtraction between two numbers. After subtracting, it returns the result. This approach is more organized, and reusable, and works well for larger programs.

Implementation Code

using System;

class Program {
    // Function to subtract two numbers
    static int SubtractTwoNumbers(int a, int b) {
        return a - b;
    }

    static void Main(string[] args) {
        int number1 = 15, number2 = 9;
        int result = SubtractTwoNumbers(number1, number2);
        Console.WriteLine("The result of subtraction is: " + result);
    }
}
    

Output

The result of subtraction is: 6

Time Complexity: O(1)
Space Complexity: O(1)

Using Class

Class is a concept used in object-oriented programming languages. OOP is a programming paradigm that organizes code into reusable blueprints called classes. These classes are used to create objects. We create a class that encapsulates the subtraction logic in its method. Finally, we create an object to call the method and pass two numbers as arguments.

Implementation Code

using System;

// Define the Subtraction class
class Subtraction {
    // Method to perform subtraction
    public int SubtractTwoNumbers(int a, int b) {
        return a - b; // Subtract and return result
    }
}

class Program {
    static void Main(string[] args) {
        // Create an object of the Subtraction class
        Subtraction obj = new Subtraction();

        // Input numbers
        int number1 = 20, number2 = 8;

        // Call the subtract method using the object
        int result = obj.SubtractTwoNumbers(number1, number2);

        // Display the result
        Console.WriteLine("The result of subtraction is: " + result);
    }
}

Output

The result of subtraction is: 12

Time Complexity: O(1)
Space Complexity: O(1)

Updated on: 2025-01-09T12:06:16+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements