C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Add Two Integers - Java Program

Method 1 - Using Scanner

import java.util.Scanner;

 

public class AddTwoIntegers {

    public static void main(String[] args) {

 

        // Create Scanner object for taking user input

        Scanner sc = new Scanner(System.in);

 

        // Asking user to enter two numbers

        System.out.print("Enter first number: ");

        int num1 = sc.nextInt();

 

        System.out.print("Enter second number: ");

        int num2 = sc.nextInt();

 

        // Adding the two numbers

        int sum = num1 + num2;

 

        // Displaying the result

        System.out.println("The sum is: " + sum);

 

        sc.close(); // Closing scanner

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Step-by-Step Explanation

  1. import java.util.Scanner;
    • Imports the Scanner class to allow input from the keyboard.
  2. Scanner sc = new Scanner(System.in);
    • Creates a Scanner object to read user input.
  3. int num1 = sc.nextInt();
    • Reads an integer entered by the user and stores it in num1.
  4. int num2 = sc.nextInt();
    • Reads another integer and stores it in num2.
  5. int sum = num1 + num2;
    • Performs the addition of num1 and num2.
  6. System.out.println("The sum is: " + sum);
    • Displays the result of the addition.
  7. sc.close();
    • Closes the Scanner object.

 

Method 2 - Without Using Scanner

public class AddTwoIntegers {

    public static void main(String[] args) {

        int num1 = 10;

        int num2 = 20;

 

        int sum = num1 + num2;

        System.out.println("Sum = " + sum);

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Method 3 - Using Methods

public class AddUsingMethod {

 

    static int add(int x, int y) {

        return x + y;

    }

 

    public static void main(String[] args) {

        int a = 15;

        int b = 25;

        int result = add(a, b);

 

        System.out.println("Sum = " + result);

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Method 4 - Add Two Integers (Using Command-Line Arguments)

public class AddUsingCLA {

    public static void main(String[] args) {

        int a = Integer.parseInt(args[0]);

        int b = Integer.parseInt(args[1]);

 

        System.out.println("Sum = " + (a + b));

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Output

 
RUN :
java AddUsingCLA 10 20

Method 5 - Add Two Integers (Using Swing GUI)

import javax.swing.*;

 

public class AddSwing {

    public static void main(String[] args) {

 

        String n1 = JOptionPane.showInputDialog("Enter first number:");

        String n2 = JOptionPane.showInputDialog("Enter second number:");

 

        int a = Integer.parseInt(n1);

        int b = Integer.parseInt(n2);

 

        int sum = a + b;

 

        JOptionPane.showMessageDialog(null, "Sum = " + sum);

    }

}


Method 6 - Add Two Integers (OOP Based)

class Addition {

    int a, b;

 

    Addition(int x, int y) {

        a = x;

        b = y;

    }

 

    int add() {

        return a + b;

    }

}

 

public class AddOOP {

    public static void main(String[] args) {

        Addition obj = new Addition(30, 40);

        System.out.println("Sum = " + obj.add());

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Method 7 - Add Two Integers (Using Constructor)

public class AddUsingConstructor {

 

    int sum;

 

    AddUsingConstructor(int x, int y) {

        sum = x + y;

    }

 

    void display() {

        System.out.println("Sum = " + sum);

    }

 

    public static void main(String[] args) {

        AddUsingConstructor obj = new AddUsingConstructor(50, 70);

        obj.display();

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Method 8 - Add Two Integers (Using Wrapper Classes)

public class AddUsingWrapper {

    public static void main(String[] args) {

 

        Integer a = 20;

        Integer b = 30;

 

        Integer sum = a + b;

 

        System.out.println("Sum = " + sum);

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Method 9 - Add Two Integers (Using BufferedReader)

import java.io.*;

 

public class AddUsingBufferedReader {

    public static void main(String[] args) throws Exception {

 

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

 

        System.out.print("Enter first number: ");

        int a = Integer.parseInt(br.readLine());

 

        System.out.print("Enter second number: ");

        int b = Integer.parseInt(br.readLine());

 

        System.out.println("Sum = " + (a + b));

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Method 10 - Add Two Integers (Using JOptionPane)

import javax.swing.*;

 

public class AddJOptionPane {

    public static void main(String[] args) {

 

        int a = Integer.parseInt(JOptionPane.showInputDialog("Enter first number"));

        int b = Integer.parseInt(JOptionPane.showInputDialog("Enter second number"));

 

        int sum = a + b;

 

        JOptionPane.showMessageDialog(null, "Sum = " + sum);

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Method 11 - Add Two Integers (Using Function With Return Value)

public class AddWithReturn {

 

    static int add(int a, int b) {

        return a + b;

    }

 

    public static void main(String[] args) {

        System.out.println("Sum = " + add(12, 18));

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35

Method 12 - Add Two Integers (Using Static Block)

public class AddUsingStaticBlock {

 

    static int a, b, sum;

 

    static {

        a = 10;

        b = 20;

        sum = a + b;

    }

 

    public static void main(String[] args) {

        System.out.println("Sum = " + sum);

    }

}

Output

 
OUTPUT :
Enter first number: 10
Enter second number: 25
The sum is: 35