Adding two numbers without using plus operator

In this example, We will show you how to write a Java program about Adding two numbers without using plus operator.

AdditionWithoutPlus.java

package com.dineshkrish;
import java.util.Scanner;
public class AdditionWithoutPlus {
public static void main(String[] args) {
AdditionWithoutPlus addition = new AdditionWithoutPlus();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first number : ");
int a = scanner.nextInt(); // Reading first number
System.out.println("Enter the second number : ");
int b = scanner.nextInt(); // Reading second number
System.out.println("Result : "+addition.add(a, b));
}
public int add(int a, int b) {
int c;
c = (a - ~b) - 1; // Adding the two integer without using (+)
return c;
}
}

Output

——————–
Enter the first number :
10
Enter the second number :
20
Result : 30

Tags:

No responses yet

Leave a Reply

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