- To print the sum of 2 numbers
User Input: 2,3
Output: 5
import java.util.Scanner;
public class SumTwoNumber {
public static void main(String []args) {
int n1,n2;
Scanner userInput = new Scanner(System.in);
System.out.print("Enter two numbers : ");
n1 = userInput.nextInt();
n2 = userInput.nextInt();
System.out.println("Sum :"+ (n1+n2));
}
}
Enter two numbers : 7 5 Sum : 12Program
Here, we use 2 variables (above code n1
& n2
) to take numbers from the user
Now, Just use the add(+) operator to print the sum of the numbers
//use add(+) operator, must use inside curly-brackets [only for this operator]
System.out.println("Sum :"+ (n1+n2));
For devision (/), subtraction (-), multiplication (*), you can use their respective operators
Concept