Core Java

Java Program to Add Two Numbers Without Using + operator

1. Introduction

In this article, You will learn how to write a java program to add two numbers without using + or ++ operators. This looks quite interesting for freshers to think beyond their capability but it is very easy for mathematic lovers. Before that in the last tutorial, we have explained how to add two numbers in java and shown the problems that occur. And also shown the example to read the input from user using Scanner.

2. Java Program for addition without + operator

Let us jump into our topic today. How can you solve this problem? As told earlier if you are good at mathematics core concepts then by this time you would have guessed the solution. The answer will be very very simple.

Clue: In the problem statement it is mentioned that should not use + operator, But still you can use remaining operators.

Before seeing the solution, first put your thoughts on papers and see the original answer.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.javaprogramto.engineering.programs;
 
import java.util.Scanner;
 
public class SumOfTwoNumbersWithOutPlusOperator {
 
    public static void main(String[] args) {
 
        // reading input from user
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter First Number : ");
        int input1 = scanner.nextInt();
        System.out.print("Enter Second Number : ");
        int input2 = scanner.nextInt();
 
        // summing two numbers
        int output = input1 -(- input2);
 
        System.out.println("Scanner example to Sum of two numbers without using + operator (" + input1 + ", " + input2 + ") = " + output);
 
    }
}

Output:

1
2
3
Enter First Number : 12
Enter Second Number : 2
Scanner example to Sum of two numbers (12, 2) = 14

Here used simple formula result = a -(-b) which is equivalent to the “a + b”.

3. Conclusion

In this tutorial, you have learned that we can still add two numbers even though not using + operator.

In the next tutorial, We will discuss and understand how to add two numbers without using any operator.

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Java Program to Add Two Numbers Without Using + operator

Opinions expressed by Java Code Geeks contributors are their own.

Venkatesh Nukala

Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button