Write a Java program to add two numbers without using any arithmetic operators

Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
public class Javaexcercise {
     public static void main(String[] arg) 
     {
       int num1, num2 ;
       Scanner in = new Scanner(System.in);	
       System.out.print("Enter 1st number: ");
       num1 = in.nextInt(); 
       System.out.print("Enter 2nd number: ");
       num2 = in.nextInt(); 
      while(num2 != 0){
            int carry = num1 & num2;
            num1 = num1 ^ num2;
            num2= carry << 1;
        }
        System.out.print("Sum: "+num1); 
        System.out.print("\n");     	
    }	
}

Result

Write a Java program to add two numbers without using any arithmetic operators
Write a Java program to add two numbers without using any arithmetic operators

Leave a Comment

x