VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-find-the-sum-and-difference-of-two-numbers/

⇱ Program to find the sum and difference of two numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find the sum and difference of two numbers

Last Updated : 29 Jan, 2024

Given two Integers A and B, your task is to find the sum and difference of two numbers.

Examples:

Input: A = 4, B = 9
Output: Sum = 13 and Difference = -5
Input: A = -3, B = -2
Output: Sum = -5 and Difference = -1

Approach:

The problem can be solved using arithmetic operators.

Operations on Numbers:

  • Addition (+): This operation is used to add the inputs to get the output. For example, when 2 is added to 3 the resultant will be equal to the sum of both the numbers which is equal to 5, (2 + 3 = 5).
  • Subtraction (-): This operation is used to subtract the inputs to get the result. For example, when 3 is subtracted by 2 the resultant will be equal to the difference of both the numbers which is equal to 1 (3 – 2 = 1).

Step-by-step algorithm:

  • Read A and B from the user
  • Calculate the sum: result = A + B
  • Calculate the difference: result = A - B
  • Display the results.

Below is the implementation of the above approach:


Output
Sum of 4 and 9 = 13
Difference of 4 and 9 = -5

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

Comment
Article Tags:
Article Tags: