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

Addition of two numbers in java using method is the most common java interview question.

In java addition program user enters two numbers as input. These two numbers are stored in integer variables and then passed as parameters to addTwoNumbers() method.

Also read – binary search in java

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