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

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. These two numbers are stored in integer variables and then passed as parameters to addTwoNumbers() method.

Also read – addition of two numbers in java without using scanner

Lastly addTwoNumbers() method returns sum of two numbers to “main” method. Here’s the java program to add two numbers taking input from user,

// addition program in java
import java.util.Scanner;
public class AddTwoNumbers
{
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();
sum = addTwoNumbers(num1, num2);
System.out.println("Sum : " + sum);
sc.close();
}
   static int addTwoNumbers(int a, int b)
   {
      return a + b;
   }
}



Output:

First number : 25
Second number : 63
Sum : 88


Similarly addition program in java can be executed without using method – click here.


Java program to add two numbers using bufferedreader

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.

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

Here’s the java program to add two numbers using bufferedreader,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AddTwoBufferedReader 
{
   public static void main(String[] args) throws NumberFormatException, IOException 
   {
      int number1, number2, add;
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Please enter two numbers to add: ");
      number1 = Integer.parseInt(br.readLine());
      number2 = Integer.parseInt(br.readLine());
      add = number1 + number2;
      System.out.println("Add two numbers using bufferedreader: " + add);
   }
}


Output:

Please enter two numbers to add:
63
26
Add two numbers using bufferedreader: 89


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) 
   {
      int number1, number2, addition;
      Scanner sc = new Scanner(System.in);
      number1 = Integer.parseInt(args[0]);
      System.out.println("First number is: " + number1);
      number2 = Integer.parseInt(args[1]);
      System.out.println("Second number is: " + number2);
      addition = number1 + 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