Java Program to add two numbers using Scanner
- May 09, 2020
- by
Java Program to add two numbers using Scanner
Logic : int c = int a + int b
In this Java Program, we use the Scanner class to get the numbers from the user.
Step 1 : Create 3 variables named as num1, num2 and sum.
Step 2 : Insert Scanner Class for taking the input from the user.
Step 3 : Use System.out.println method to take input of first number from the user.
Step 4 : Store the user's input in variable num1
Step 5 : Use System.out.println method to take input of second number from the user.
Step 6 : Store the user's input in variable num2
Step 7 : Close the Scanner.
Step 8 : Perform the addition of the two numbers entered by the user with the help of "+" operator and store the result in the variable named as sum.
Step 9 : Display the result by using the System.out.println method.
Program :
import java.util.Scanner;
public class AddTwoNumbers2
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number:");
num1 = sc.nextInt();
System.out.println("Enter Second Number:");
num2 = sc.nextInt();
sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers:"+sum);
}
}
Output:
Enter First Number :
121
Enter Second Number:
19
Sum of these numbers: 140
For execution of the code do visit my YouTube channel:


0 comments:
Post a Comment