Add two number Java program

Here is the program to add two number in java, by both without and with input by user.

Without input program

Program

//Java program to add two number
class AddNum{
    int sum;
    void add(int num1, int num2){
        sum = num1 + num2;
        System.out.println("Sum = "+sum);
    }
}

class Main{
    public static void main(String s[]){
         AddNum ob = new AddNum();
         ob.add(12,11);
    }
}

Output

Sum = 23

With Input by user

Program

//Java program to add two number
import java.util.Scanner;
class AddNum{
    int sum;
    void add(int num1, int num2){
        sum = num1 + num2;
        System.out.println("Sum = "+sum);
    }
}

class Main{
    public static void main(String s[]){
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter 1st number:");
        int num1 = sc.nextInt();
        
        System.out.println("Enter 2nd number:");
        int num2 = sc.nextInt();
        
        AddNum ob = new AddNum();
        ob.add(num1, num2);
    }
}

Output

Enter 1st number:5
Enter 2nd number:6
Sum = 11