Blog

Taking Users Input to Add 2 no.s in Java

Java / Java Programs

Taking Users Input to Add 2 no.s in Java

[vc_row][vc_column][vc_column_text]

Taking Users Input to Add 2 no.s in Java- Using Scanner Class Method

Now, In this Java Program of Taking Users Input to Add 2 no.s in Java, you should have the knowledge of How Scanner Class Works also you should know following topics:

1.Input, Output, and Import
2.Variables and Data Types
3.Operators
In Java, there are varieties of options available to accept users input & three most popular of them are:-
(a) Command Line Arguments.
(b) Using the Scanner Class.
(c) Using GUI Components.

Using Scanner Class

#Taking Users Input to Add 2 no.s in Java
import java.util.Scanner;
class AddUserInputs
{
	public static void main(String args[])
	{
		int num1,num2,sum;
		Scanner kb=new Scanner(System.in);
		System.out.println("Enter 2 no.s:");
		num1=kb.nextInt();
		num2=kb.nextInt();
		sum = num1 + num2;
        System.out.println("Sum of "+num1 +" and " +num2 +" is: "+sum);
	}
}

Output:

Enter 2 no.s:
25
47
Sum of 25 and 47 is: 72

In the Above method of Taking Users Input to Add 2 no.s in Java, we’re Importing a Scanner Class, Yes we do need to import Scanner class before using it
To do this, we use import keyword, Since Scanner class is inside the package called java.util,
that’s why we write import java.util.Scanner
Most Importantly as mentioned above Java uses all the mathematics operation therefore by Changing (+) operator, we can subtract (-), multiply (*), divide (/), floor divide (//) or find the remainder (%) of two numbers.
Related Program
1.Java Program to find the area of Triangle
2.Java Program to Solve Quadratic Equation
3.Java Program to Generate a Random Number
Ask your questions and clarify your/others doubts by commenting. Java Documentation[/vc_column_text][/vc_column][/vc_row]

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare