Java Program to add two numbers


In this program, You will learn how to add two numbers using with scanner class in Java.


20 = 10 + 10

40 = 20 + 20

Example: How to add two numbers in java.

import java.util.Scanner;

class Main{
    public static void main(String args[]) {

        int a, b, s;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter two numbers:");
        a = sc.nextInt();
        b = sc.nextInt();

        s = a + b;

        System.out.println("Sum is:" + s);

    }
}

Output:

Enter two numbers:10
20
Sum is:30