Table of Contents
ToggleIn this article, we will discuss a simple yet important concept that is addition of two numbers in Java. We will see different ways and java program to add two numbers for each of these methods. Let’s Get Started
Java Program To Add two numbers
1. Using Addition operator
Predefined Values
To add two numbers we can directly use + operator which java provides. Let’s understand directly by developing a Java Program To Add two numbers:
// Java Program To Add two numbers public class AdditionOfTwoNumbers { public static void main(String[] args) { int a = 15; int b = 20; int sum = a + b; System.out.println("Sum of two numbers is" + sum); } }
Input as Argument
In the above program the value was hard coded(predetermined). But what if we want to get value as argument to our program.Java stores the arguments in String[] args String array. So each argument can be accessed by its index.
Remember, since it is a array of strings, thus we need to first convert string to integer. For that we will use Integer.parseInt() function.
Let’s look at the java program for this.
// Java Program To Add two numbers public class AdditionOfTwoNumbers { public static void main(String[] args) { //Convert arguments to integer int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int sum = a + b; System.out.println("Sum of two numbers is" + sum); } }
Input from User
Now, let’s say we want to take input of these number to be added from user. For that we will use Scanner Class and for taking integer as input we will use nextInt() function.
// Java Program To Add two numbers import java.util.Scanner; public class AdditionOfTwoNumbers { public static void main(String[] args) { //System.in is a standard input stream Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); int a = sc.nextInt(); System.out.print("Enter second number: "); int b = sc.nextInt(); int sum = a + b; System.out.println("Sum of two numbers is" + sum); } }
2. Using Subtraction operator:
Since negation of negative is positive, we can use this to add two numbers. Have a look at the program.
// Java Program To Add two numbers public class AdditionOfTwoNumbers { public static void main(String[] args) { int a = 15; int b = 20; //Negation of negative int sum = a - (-b); System.out.println("Sum of two numbers is " + sum); } }
3. Using Unary Operator
The idea is to loop until first number becomes zero and keep incrementing its corresponding second operand by the same amount of iterations if first number is positive else decrement by the same amount of iterations
// Java Program To Add two numbers public class AdditionOfTwoNumbers { public static void main(String[] args) { System.out.println("Sum of two numbers is: " + add(15, 20)); System.out.println("Sum of two numbers is: " + add(-15, 20)); } public static int add(int a, int b) { // If a is positive while (a > 0) { b++; a--; } // If a is negative while (a < 0) { b--; a++; } return b; } }
4. Using Bitwise Operator In Java
The addition of two integers can carried by using XOR bitwise operator and carry can be obtained by AND operator. To add carry into sum we need to use signed left shift operator.
// Java Program To Add two numbers public class AdditionOfTwoNumbers { static int add(int a, int b) { // Iterate till there is no carry while (b != 0) { // obtain carry using AND operator int carry = a & b; // obtain sum by using XOR operator a = a ^ b; // Carry is shifted by one so that adding it to a gives the required sum b = carry << 1; } return a; } public static void main(String arg[]) { System.out.println("Sum of two numbers is: " + add(15, 20)); System.out.println("Sum of two numbers is: " + add(-15, 20)); } }
5. Recursion
Two numbers can be added using recursion too. Idea is similiar to using unary operator but instead of looping we will use recursion. We will keep incrementing second number (decrementing if first number is negative) till first number is zero. Thus, are base case will be when first number becomes zero.
// Java Program To Add two numbers public class AdditionOfTwoNumbers { static int add(int a, int b) { if (a == 0) { return b; } if (a > 0) { return add(a - 1, b + 1); } return add(a + 1, b - 1); } public static void main(String arg[]) { System.out.println("Sum of two numbers is: " + add(15, 20)); System.out.println("Sum of two numbers is: " + add(-15, 20)); } }
Similarly, recursion can be applied to bitwise technique as well.
// Java Program To Add two numbers public class AdditionOfTwoNumbers { public static int add(int a, int b) { if (b == 0) return a; int sum = a ^ b; int carry = (a & b) << 1; return add(sum, carry); } public static void main(String arg[]) { System.out.println("Sum of two numbers is: " + add(15, 20)); System.out.println("Sum of two numbers is: " + add(-15, 20)); } }
Conclusion
This was all for java program to add two numbers with using + operator and without using + operator. Hope you got the idea and got clarity on various ways to add two numbers in java.
Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!