Add Two Numbers

Read two numbers from the user, add them, and display the result.

JavaBeginner
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");
        double a = sc.nextDouble();

        System.out.print("Enter second number: ");
        double b = sc.nextDouble();

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

        sc.close();
    }
}

Output

Enter first number: 10
Enter second number: 20
Sum = 30.0

This program demonstrates:

  • Reading input from the keyboard using Scanner
  • Using double to handle decimal values
  • Performing addition and printing the result