As we already know it’s a coding language that helps us a lot in computer language or creating many things. and now we are going to learn how to add two numbers, In Java, finding the sum of two or more numbers is very easy. First, declare and initialize two variables to be added. Another variable to store the sum of digits.
Example
int x = 4;
int y = 6;
int sum = x + y;
System.out.println(sum); // Print the sum of x + y
Add Two Numbers with User Input
Here you can learn how you can add two numbers when you are going to use user input.
class MyClass {
public static void main(String[] args) {
int x, y, sum;
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Type a number:");
x = myObj.nextInt(); // Read user input
System.out.println("Type another number:");
y = myObj.nextInt(); // Read user input
sum = x + y; // Calculate the sum of x + y
System.out.println("Sum is: " + sum); // Print the sum
}
}