Java Program to Add Two Integers

We will see three programs to add two Integers, In the first program, we will use pre-define Integers in the program itself.

Sample input 1:

First number : 56

Second number : 48

Sample Output 1:

Total is : 104

Sample input 2:

First number : 19

Second number : 50

Sample Output 2:

Total is : 69

First Example: Sum of Two Integers

import java.util.*;

public class AddTwoNumber{
  public static void main (String[] args){
   
     //define variable  
     int num1=5;
     int num2=15;
     sum;
         //Arithmetic operation
         sum = num1 + num2;

        //print output
        System.out.println("Sum of these Integers : "+sum);
  }
}

Output :

Sum of there Integers : 20

Second Example: Sum of two integers using Scanner

Here we will take user interaction in the program by taking input from the user. The Scanner allows us to capture the user input so that we can get values from the user. The program then calculates the sum and display values.

import java.util.*;
import java.util.Scanner;

public class AddTwoNumbers2 {
    public static void main(String[] args) {
        
        //define variable
        int num1;
        int num2;
        int sum;

        //define scanner
        Scanner sc = new Scanner(System.in);

        //using Scanner accepting user input
        System.out.println("Enter First Integer: ");
        num1 = sc.nextInt();
        
        System.out.println("Enter Second Integer: ");
        num2 = sc.nextInt();
  
       //close Scanner
        sc.close();

        //Arithmetic operation
        sum = num1 + num2;

        //print output
        System.out.println("Sum of these Integer : "+sum);
    }
}

Output :

Enter First Integer:
120
Enter Second Integer:
20
Sum of these Integers: 140

Third Example: Sum of two number with using + operator

Here we will use the function Add() instead of using arithmetic operators. The Function should not use any of the arithmetic operations (+,++, -, etc). The Sum of two integers can be performed using XOR (^) of the Integers. Carry bit can be obtained by performing AND (&) of two bits.

import java.util.*;
import java.util.Scanner;

AddTwoNumbers3{

    static int Add(int x, int y) 
    { 
        // Iterate till there is no carry 
        while (y != 0)  
        {  
            // set bits of x and y 
            int carry = x & y; 
  
            // Sum of bits of x and  
            // y where at least one  
            x = x ^ y; 
  
            // Carry is shifted by  
            y = carry << 1; 
        } 
        return x; 
    } 
      
    // Driver code 
    public static void main(String arg[])  
    { 
        //define variables
        int num1;
        int num2;
        int sum;
        
        //define Scanner
        Scanner sc = new Scanner(System.in);
        
        //using Scanner accepting user input
        System.out.println("Enter First Integer: ");
        num1 = sc.nextInt();
        
        System.out.println("Enter Second Integer: ");
        num2 = sc.nextInt();
        
       //close Scanner
        sc.close();
         
        System.out.println("Sum of these Integer : "+(Add(num1, num2)));
        
        
    } 
    
}

Output :

Enter First Integer:
150
Enter Second Integer:
20
Sum of these Integers: 170

Recommended

  1. Welcome Message
  2. Alien’s Visit
  3. News Report Generation
  4. Introducing Artificial Intelligence System
  5. Stationary Shop

Leave a Comment

Your email address will not be published. Required fields are marked *

x