0542-2316689 | [email protected]

Taking Users Input to Add 2 no.s in Java

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

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:");

		sum = num1 + num2;
        System.out.println("Sum of "+num1 +" and " +num2 +" is: "+sum);


Enter 2 no.s:
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

0 responses on "Taking Users Input to Add 2 no.s in Java"

Leave a Message

Your email address will not be published. Required fields are marked *

© 2017-18 TCA