In this post, we will see how to add two numbers in java. This is the most basic program of java programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; public class Calculator { public static void main(String args[]) { int num1, num2, sum; // Taking input from user Scanner scanner = new Scanner(System.in); System.out.print("Enter two numbers : "); num1 = scanner.nextInt(); num2 = scanner.nextInt(); // Adding two numbers sum = num1 + num2; System.out.print("Sum of two numbers is " +sum); } } |
Output:
Enter two numbers : 5 7
Sum of two numbers is 12
Sum of two numbers is 12
If you notice, we have use Scanner to take input from user.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.