# Java program to add two numbers using method

Let’s learn java program to add two numbers using method.

## Java program to add two numbers using method

Simple java program for adding two numbers is the most common java interview question.

In addition program in java using methods user enters two numbers as input using nextInt() method of Scanner class.

These two numbers are stored in integer variables ‘num1’ and ‘num2’ and then passed as parameters to addTwoNumbers() method.

Lastly addTwoNumbers() method returns sum of two numbers to “main” method.

Here’s the java program to add two numbers taking input from user or java program to add two numbers using method.

```// addition program in java
import java.util.Scanner;
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("First number : ");
num1 = sc.nextInt();
System.out.println("Second number : ");
num2 = sc.nextInt();
System.out.println("Sum : " + sum);
sc.close();
}
public static int addTwo(int a, int b)
{
int sum = a + b;
return sum;
}
}

```

Output:

First number : 25
Second number : 63
Sum : 88

In java program to add two numbers using bufferedreader first get input from user using BufferedReader input line.

In the next step convert string to integer using Integer.parseInt() method.

This method returns the integer value represented by the argument in decimal.

Also read – continue statement in java

Finally addition operator (+) is performed on two numbers and the result is stored in ‘add’ variable.

```import java.io.BufferedReader;
import java.io.IOException;
{
public static void main(String[] args) throws NumberFormatException, IOException
{
}
}

```

Output:

63
26

Addition of two numbers in java using command line arguments

Let’s learn to add two numbers by passing command line arguments.

```import java.util.Scanner;
public class CommandLineArguments
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
number1 = Integer.parseInt(args);
System.out.println("First number is: " + number1);
number2 = Integer.parseInt(args);
System.out.println("Second number is: " + number2);
System.out.println("Addition of two numbers in java using command line arguments: " + addition);
sc.close();
}
}

```

Output:

PS C:\Output> java CommandLineArguments 10 40
First number is: 10
Second number is: 40
Addition of two numbers in java using command line arguments: 50

Multiplication of two numbers in java

Here’s the java program on multiplication of two numbers in java.

```import java.util.Scanner;
public class MultiplicationOfTwoNumbers
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter first number to multiply: ");
int number1 = sc.nextInt();
System.out.println("Please enter second number to multiply: ");
int number2 = sc.nextInt();
int multiplication = number1 * number2;
System.out.println("Multiplication of two numbers in java: " + multiplication);
sc.close();
}
}

```

Output:

Please enter first number to multiply: 5
Please enter second number to multiply: 8
Multiplication of two numbers in java: 40