Java Program to Add Two Numbers

In this blog, you will learn how to add two numbers in java. The resultant sum is displayed on the screen. Adding two numbers often seems to be too easy. We will discuss how to find the sum or addition of two numbers. We have also discussed different ways through which users can provide input, and much more.

Algorithm for Sum of two numbers in Java:

  • Declare two variables.
  • Initialize the variables with some values.
  • Declare another variable to store the sum of the numbers.
  • Apply the mathematical plus(+) operator between two values and store the result.

Method 1: Standard java program to add two numbers

class SumOfNumbers
{  
	public static void main(String args[])   
	{  
		int A = 25, B = 25, sum;  
		sum = A + B;  
		System.out.println("The sum of two numbers is: "+sum);  
	}  
}  
Output: The sum of two numbers is: 50

This is a straightforward approach to finding the sum of two numbers let’s have a look at how to find the sum of two numbers in java using the method.

There are two ways to find the sum of two numbers in java using the method

  • By using the user-defined method
  • By using the sum() method

Method 2: By using the user-defined method:

Java allows the user to read the input. We can take two numbers as input from the user and pass them to the user-defined method sum().
Below is the implementation of the program.

import java.util.Scanner;  
class SumOfNumbers2  
{  
	public static void main(String args[])  
	{  
		int x, y, sum;  
		Scanner sc = new Scanner(System.in);  
		x = sc.nextInt();  
		y = sc.nextInt(); 
		sum = sum(x, y);  
		System.out.println("The sum of numbers " + x + " and " + y + " is: " +sum );  
	}   
	public static int sum(int a, int b)  
	{  
		int sum = a + b;  
		return sum;  
	}  
}  
Output: The sum of numbers 10 and 20 is: 30

Method 3: By using Integer.sum() method:

The integer class provides the sum() method. sum() method is a static method that adds two numbers as per operator.

Syntax: public static int sum(int a, int b);
class SumOfNumbers
{    
	public static void main(String args[])   
	{            
		int x = 10, y = 20, a = 5, b = 5, sum, s;    
		sum=Integer.sum(x, y);  
		System.out.println("The sum of x and y is: " +sum);    
		s=Integer.sum(a, b);  
		System.out.println("The sum of x and y is: " +s);  
	}    
}   
Output:
The sum of x and y is: 30
The sum of x and y is: 10

Method 4: Java program to add two numbers using command line arguments

Another method to taking input from user is command line arguments. We will discuss the java program to add two numbers where both the numbers are provided by the user using arguments from the command line.

class SumOfNumbers4  
{  
public static void main(String args[])  
{  
int x = Integer.parseInt(args[0]); //first arguments   
int y = Integer.parseInt(args[1]); //second arguments  
int sum = x + y;  
System.out.println("The sum of x and y is: " +sum);  
}  
} 
Output:
The sum of x and y is: 100

Explanation:

Compile the above program using the command javac SumofNumbers.java. After that, run the program by using command java SumofNumbers 50 50.
Where 50 and 50 are command line arguments.

Conclusion:

In this article, we have studied how to find the sum of two numbers in Java using different methods. Along with that we have also learned how to take input from user. We hope this blog has helped you to enhance your knowledge regarding the java programming language.

Other Java Programs
Java program to find the factorial of a number
Java program to search an element in a linked list
Java program to convert arraylist to linkedlist
Java program to reverse a linked list

Leave a Reply

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