Java program to add two numbers using method

Hey guys!! Welcome to flower brackets blog. In this post let’s see java program to add two numbers using method.

In this 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 – java program to add two numbers using BigInteger class

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

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

Related Posts