Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Java Program to Add Two Numbers

Last Updated on May 9, 2023 by Prepbytes

You will learn how to create a Java program that adds two integers in this tutorial. This is a very simple Java program that asks the user to enter two integers, saves those inputs in two different variables, and then shows the total of the two values.

To determine the sum of two integer numbers provided by the user, we shall create two variables. The user is prompted to enter two integer values in the first variable, after which the variable shows the total of the two integers. Using a user-defined function, we are carrying out the identical action in the second C program.

What is Addition of Two Numbers in Java?

In the Java programming language, adding two integers entails taking two integer numbers, adding them, and then printing the result. Let’s look at the Java programming language’s technique for adding two numbers, which is a straightforward process.

Algorithm for Addition of Two Numbers in Java

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

Method 1: Standard Program for the Addition of Two Numbers in Java

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 method of computing the sum of two numbers in Java is simple. Let’s look for the sum of two numbers in Java using methods.

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

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

Method 2: Sum of Two Numbers in Java by using the User-defined Method

Java allows the reading of user input. The user-defined method sum() can accept two numbers from the user as input.
The Java program’s implementation to add two numbers is shown below.

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: Addition of Two Numbers in Java by using Integer.sum() Method

The sum() method is available from the integer class. A static method called sum() adds two numbers according to the 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: Addition of Two Numbers in Java by 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:
We had discussed four different approaches for the Java program to add two numbers. Along with that we have also learned how to take input from users. Addition of two numbers in Java is a simple program, but there are many different approaches for the addition of two numbers in Java. You have to figure out more approaches for the addition of two numbers in Java.

FAQ For Java Program To Add Two Numbers

Q1. What is sum += in Java?
Ans. Java uses the compound addition assignment operator, which looks like: += (plus sign, followed by equal sign). sum += 10 ; // Add 10 to sum.

Q2. What is a keyword in Java?
Ans. A Java keyword is one of 50 reserved terms that have a special function and a set definition in the Java programming language. The fact that the terms are reserved means that they cannot be used as identifiers for any other program elements, including classes, subclasses, variables, methods and objects.

Q3. What is the method in Java?
Ans. A method in Java is a block of code that, when called, performs specific actions mentioned in it. For instance, if you have written instructions to draw a circle in the method, it will do that task. You can insert values or parameters into methods, and they will only be executed when called.

Q4. What is a class in Java?
Ans. A class in Java is a logical template to create objects that share common properties and methods. Hence, all objects in a given class will have the same methods or properties. For example: in the real world, a specific cat is an object of the “cats” class.

Other Java Programs

Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List

Leave a Reply

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