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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2. public class Javaexcercise {
  3. public static void main(String[] arg)
  4. {
  5. int num1, num2 ;
  6. Scanner in = new Scanner(System.in);
  7. System.out.print("Enter 1st number: ");
  8. num1 = in.nextInt();
  9. System.out.print("Enter 2nd number: ");
  10. num2 = in.nextInt();
  11. while(num2 != 0){
  12. int carry = num1 & num2;
  13. num1 = num1 ^ num2;
  14. num2= carry << 1;
  15. }
  16. System.out.print("Sum: "+num1);
  17. System.out.print("\n");
  18. }
  19. }

Result

java techstudy.org

     





Related Topics




Popular Posts