Java program to add two numbers without using addition operator

In this tutorial you will learn writing program to add two numbers without using addition operator in Java.

 

Read This:  Write a program in C to add two number without using third variable.

This program will take two number as a Input. And After performing some operation as per program, it will return addition.

Suppose if we give input 35 and 20. Our program will return 55 as an output without using of an addition operator.

Below is a program to add two numbers without using + operator in Java

import java.util.Scanner;
public class Main {
     public static void main(String[] arg) 
	 {
	   int x, y ;
	   Scanner sc = new Scanner(System.in);	
	   System.out.print("Please Give first number: ");
	   x = sc.nextInt(); 
	   System.out.print("Please Give second number: ");
	   y = sc.nextInt(); 
      while(y != 0){
            int temp = x & y;
            x = x ^ y;
            y = temp << 1;
        }
        System.out.print("Sum = "+x); 
		System.out.print("\n");     	
	}	
} 

Output:

add two number without using + in java

Leave a Comment