Program to find the sum and difference of two numbers Last Updated : 29 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Given two Integers A and B, your task is to find the sum and difference of two numbers. Examples: Input: A = 4, B = 9Output: Sum = 13 and Difference = -5Input: A = -3, B = -2Output: 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 userCalculate the sum: result = A + BCalculate the difference: result = A - BDisplay the results.Below is the implementation of the above approach: C++ #include <iostream> using namespace std; int main() { int A, B; A = 4; B = 9; cout << "Sum of " << A << " and " << B << " = " << A + B << endl; cout << "Difference of " << A << " and " << B << " = " << A - B << endl; } Java public class MainClass { public static void main(String[] args) { // Declaring and initializing variables A and B int A, B; A = 4; B = 9; // Printing the sum of A and B System.out.println("Sum of " + A + " and " + B + " = " + (A + B)); // Printing the difference of A and B System.out.println("Difference of " + A + " and " + B + " = " + (A - B)); } } Python3 # Python code A = 4 B = 9 # Printing the sum of A and B print(f"Sum of {A} and {B} = {A + B}") # Printing the difference of A and B print(f"Difference of {A} and {B} = {A - B}") C# using System; class MainClass { public static void Main(string[] args) { int A, B; A = 4; B = 9; // Printing the sum of A and B Console.WriteLine($"Sum of {A} and {B} = {A + B}"); // Printing the difference of A and B Console.WriteLine($"Difference of {A} and {B} = {A - B}"); } } JavaScript let A, B; A = 4; B = 9; console.log("Sum of " + A + " and " + B + " = " + (A + B)); console.log("Difference of " + A + " and " + B + " = " + (A - B)); OutputSum of 4 and 9 = 13 Difference of 4 and 9 = -5Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to find the sum and difference of two numbers V vaibhav_gfg Follow Improve Article Tags : DSA Similar Reads Find two numbers from their sum and OR Given two integers X and Y, the task is to find two numbers whose Bitwise OR is X and their sum is Y. If there exist no such integers, then print "-1". Examples: Input: X = 7, Y = 11Output: 4 7Explanation:The Bitwise OR of 4 and 7 is 7 and the sum of two integers is 4 + 7 = 11, satisfy the given con 6 min read Program to Subtract two integers of given base Given three positive integers X, Y, and B, where X and Y are Base-B integers, the task is to find the value of X - Y such that X >= Y. Examples: Input: X = 1212, Y = 256, B = 8Output: 0734Explanation: The value of 1212 - 256 in base 8 is 734. Input: X = 546, Y = 248, B = 9Output: 287 Approach: Th 12 min read To find sum of two numbers without using any operator Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used.Solution It's a trick question. We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in 9 min read Program to add two integers of given base Given three integers X, Y, and B, where X and Y are Base-B integers. The task is to find the sum of integers X and Y. Examples: Input: X = 123, Y = 234, B = 6 Output: 401 Explanation: Sum of two integers in base 6 - 1 1 1 2 3 + 2 3 4 ------------- 4 0 1 Input: X = 546, Y = 248 B = 9 Output: 805 Expl 9 min read Possible two sets from first N natural numbers difference of sums as D Given N and D, find if it is possible to make two sets from first N natural numbers such that the difference between the sum of 2 sets(individually) is D. Examples : Input : 5 7 Output : yes Explanation: Keeping 1 and 3 in one set, and 2, 4 and 5 are in other set. Sum of set 1 = 4 Sum of set 2 = 11 4 min read Like