Start Coding Now
🌱 Beginner Programsbeginner1 methods

Add Two Numbers in Java

Simple Java program to add two numbers. Learn how to take user input and perform arithmetic operations.

Last updated: 11 January 2026

Method 1: Using Scanner

Taking input from user.

import java.util.Scanner;

public class AddNumbers {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        
        System.out.print("Enter two numbers: ");
        int first = reader.nextInt();
        int second = reader.nextInt();
        
        int sum = first + second;
        
        System.out.println("The sum is: " + sum);
    }
}
Output:
Enter two numbers: 10 20
The sum is: 30

Explanation

We use the Scanner class to read two integers from standard input and then the + operator to calculate their sum.

Frequently Asked Questions

Try This Program

Copy this code and run it in our free online Java compiler.

Open Java Compiler