Open In App

Program to find the sum and difference of two numbers

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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));


Output

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







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



Similar Reads

Program to find the average of two numbers
Given two Integers A and B, your task is to find the average of two numbers where the average is given by the formula:average(A, B) = (A+B)/2 Examples: Input: A=5, B=3Output: 4Explanation: Average(A,B) = (A+B)/2 = 8/2 =4 Input: A=10, B=5Output: 7.5Explanation: Average(10, 5) = (10 + 5)/2 = 15/2 = 7.5 Approach: To solve the problem, follow the below
3 min read
Program to print all two-digit numbers in descending order
Write a program to print all the two-digit numbers in descending order, that is print all the two-digit numbers from largest to smallest. Output format: 99 98 97 96 95 94 93 ..... 13 12 11 10 Approach: We know that the largest two-digit number is 99 and the smallest two-digit number is 10. So, we can start from 99 and then keep on decreasing the nu
2 min read
Program to print first 10 even numbers
Program to print first 10 even numbers. A number is even if it is divisible by 2 for example 4, 100, 24 etc. Output Format: 0 2 4 6 8 10 12 14 16 18 Approach: Checking Parity using Modulo operator(%) Using the modulo % operator we can find the remainder of any number when divided by 2, giving us the parity according to two cases: remainder = 0: Eve
3 min read
Program to print all three digit numbers in ascending order
Write a program to print all the three-digit numbers in ascending order. Output Format: 100 101 102...till 999 Approach: Using for Loop to print all the three Digit Numbers: Use for loop to iterate from the smallest three-digit number, that is 100 till we reach the largest three-digit number, that is 999 and, in every iteration, print the number. A
3 min read
Program to print first 10 prime numbers
Write a program to print the first 10 prime numbers.Note: A number N is said to be prime if it has exactly two factors i.e. 1 and the number itself N Output Format: 2, 3, 5, 7, 9... Approach: Prime Test: To check whether a number N is prime we can check its divisibility with each number from 2 to N - 1, If it is divisible by any number in this rang
4 min read
Program to print numbers having remainder 3 when divided by 11
Write a program to print all numbers from 1 to 1000 which leave a remainder of 3 when divided by 11. Output format: 3, 14, 25, 36, 47.... 993 Approach: To solve the problem, follow the below idea: The problem can be solved by simply using for loop and iterate from 3 up to the 1000 and increment by 11 as we are dividing by 11. Step-by-step approach:
4 min read
Program to print first 10 numbers of Fibonacci series
Find the first 10 numbers of Fibonacci series. The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, ........ Approach: To solve the problem, follow the below idea: We can take two variable
3 min read
Program to count the number of days between two years
Given two years. Write a program to count the number of days between the two years. Examples: Input: startYear = 1990, endYear = 2001Output: 4383 Explanation: Years 1992, 1996 and 2000 are leap year and all 9 others are non-leap years. So, total number of days are: 366 * 3 + 365 * 9 = 4383 Input: startYear = 1890, endYear = 1998Output: 39811 Approa
4 min read
Program to generate a random two-digit number
Write a program to generate a random two-digit number. Example 1: 73Example 2: 26 Approach: To solve the problem, follow the below idea: We can generate a random two-digit number by generating a random integer first and then modulo it by 90. Now, after modulo 90 the remainder can be in the range 0 to 89, so in order to have the remainder as a two-d
2 min read
Program to count the number of months between given two years
Given a start year and end year. Write a program to count number of months between given two years (both inclusive). Examples: Input: startYear = 1991, endYear = 2023Output: 396Explanation: The number of years between 1991 and 2023 (both inclusive) are: 33. So, total number of months = 396. Input: startYear = 2010, endYear = 2023Output: 168Explanat
4 min read
Article Tags :