Java Program to Add Two Integers

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Program to Add Two Integers

In this article we will see how we can add two integers in multiple ways.

So, let’s start one by one.

Method-1 : Using the “+” operator

We normally use the + operator to add two numbers.

Below is the implementation code.

import java.util.Scanner;
class sum
{
    public static void main(String args[])
    {

        int sum, a, b ;
        //Creating the scanner class to take input from the user
        Scanner obj = new Scanner(System.in);
        a = obj.nextInt();
        b = obj.nextInt();
        //Adding both integers
        sum =  a + b;
        System.out.println("The sum of "+a+" and "+b+" is = "+sum);

    }
}
Output :

Enter two integers to add
10
-21
The sum of 10 and -21 is = -11

Method-2 : By the double minus operator logic :

We can use the double minus logic that we use in maths i.e.

-(- ) = +

The approach will be the same as the first method we will only replace the sum logic.

Below is the implementation code.

import java.util.Scanner;
class sum
{
    public static void main(String args[] )
    {

        int sum, a, b ;
        //Creating the scanner class to take input from the user
        Scanner obj = new Scanner(System.in);
        a = obj.nextInt();
        b = obj.nextInt();
        //Adding both integers
        sum =  a - (-b);
        System.out.println("The sum of "+a+" and "+b+" is = "+sum);

    }
}
Output:

Enter two integers to add
10
11
The sum of 10 and 11 is = 21

Method-3 : By using the sum( ) library function

We can add using the sum( ) function which is present in Integer class inside the java.lang package.

Syntax- Integer.sum(variable_1, variable_2);
import java.lang.Integer;
class sum
{
    public static void main(String args[] )
    {

        int sum, a = 15, b = 20 ;
        //Adding both integers using the library function
        sum =  Integer.sum(a,b);
        System.out.println("The sum of "+a+" and "+b+" is = "+sum);

    }
}

Output:

The sum of 15 and 20 is = 35

Method-4 : By incrementing and decrementing method

To add two numbers using the incrementing/decrementing method we will be using the following approach.

Approach:

  • Initialize two variables ‘a’ and ‘b’ with integer values
  • Run a while loop which runs until ‘a’ is greater than zero
  • Inside the loop increment ‘b’ and decrement ‘a’.
  • So when ‘b’ will be incremented with 1, ‘a’ times then it will result addition of ‘a’ and ‘b’.
  • Display the result

Below is the implementation code.

import java.lang.Integer;
class sum
{
    public static void main(String args[] )
    {

        int  a = 15, b = 20 ;
        System.out.print("The sum of "+a+" and "+b+" is = ");
        //Adding both integers using increment and decrement operators
        while(a>0)
        {
        //Decrementing a while incrementing b
            b++;
            a--;
        }
        System.out.print(b);
    }
}
Output:

The sum of 15 and 20 is = 35

Method-4 : Using Recursion

We can use recursion to call the function until the number becomes zero.

Recursion: When a function call itself again and again is called recursion.

Below is the implementation code.

import java.util.*;

class recursion
{
    //Function to add two numbers
    public static int addNums(int num1, int num2)
{
    if (num1 > 0)
        return num2;
    // Recursive function call
    else
        return addNums((num1 & num2) << 1, num1 ^ num2);
}
    public static void main(String args[])
    {
        int a =10, b= 15;
        System.out.println("The sum of "+a+ " and "+b+" is = "+addNums(a,b));
    }
}
Output:

The sum of 10 and 15 is = 35

Method-6 : Using log and exponential functions

We can also use the log and exponential functions to find out the sum of two integers in the following approach.

Approach:

  • First of all we will take two integer values
  • Then we will find the exponential function using exp( ) and multiply them,
  • Then we will find the log of the product and typecast it to int (we need to convert it to integer as the function returns a floaf type)
  • Output the sum

Below is the implementation code.

import java.lang.Math;

class sum{
    public static void main(String args[] )
    {
        int a = 5, b =10;
        //We are finding the exponential value of two numbers
        //and then multiplying them. Then finding the log of the product and
        //typecasting it to integer datatype
        int sum = (int)Math.log(Math.exp(a)*Math.exp(b));
        System.out.print("The sum of "+a+" and "+b+" is = "+ sum);
    }
}
Output:

The sum of 5 and 10 is = 15

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Basic Programs: