In this tutorial, we will discuss the concept of Java program to add two numbers without using arithmetic operators
In this post, we will learn how to make the addition of two number using without the arithmetic operator in Java programming language
This program allows the user to enter two numbers and displays the sum of two numbers entered by the user using the for loop
import java.util.Scanner; class AddNumWithOutPlus1{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number: "); int num1=scan.nextInt();//get input from the user for num1 System.out.print("Enter the second number: "); int num2=scan.nextInt();//get input from the user for num2 for(int i=0; i<num2; i++) num1++; System.out.print("Sum of two numbers is: "+num1); } }
When the above code is executed, it produces the following results
Enter the first number: 54 Enter the second number: 66 Sum of two numbers: 120
Methods
This program allows the user to enter two numbers and displays the sum of two numbers entered by the user using the while loop
import java.util.Scanner; class AddNumWithOutPlus{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number: "); int num1=scan.nextInt();//get input from the user for num1 System.out.print("Enter the second number: "); int num2=scan.nextInt();//get input from the user for num2 while(num2 != 0){ int num3=(num1 & num2); num1=num1^num2; num2=num3<<1; } System.out.print("Sum of two numbers is: "+num1); } }
When the above code is executed, it produces the following results
Enter the first number: 24 Enter the second number: 36 Sum of two numbers: 60
Methods
This program allows the user to enter two numbers and displays the sum of two numbers entered by the user using for loop inside the method
import java.util.Scanner; class AddNumWithOutPlus3{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number: "); int n1=scan.nextInt();//get input from the user for num1 System.out.print("Enter the second number: "); int n2=scan.nextInt();//get input from the user for num2 System.out.print("Sum of two numbers is: "+add(n1,n2)); } static int add(int num1,int num2){ for(int i=0; i<num2; i++) num1++; return num1; } }
When the above code is executed, it produces the following results
Enter the first number: 24 Enter the second number: 56 Sum of two numbers: 80
Method
This program allows the user to enter two numbers and displays the sum of two numbers entered by the user using while loop inside the method
import java.util.Scanner; class AddNumWithOutPlus4{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number: "); int n1=scan.nextInt();//get input from the user for num1 System.out.print("Enter the second number: "); int n2=scan.nextInt();//get input from the user for num2 System.out.print("Sum of two numbers is: "+add(n1,n2)); } static int add(int num1,int num2){ while(num2 != 0){ int num3=(num1 & num2); num1=num1^num2; num2=num3<<1; } return num1; } }
When the above code is executed, it produces the following results
Enter the first number: 67 Enter the second number: 43 Sum of two numbers: 110
Method
Similar post
C program to add two numbers without using arithmetic operators
C++ program to add two numbers without using arithmetic operators
Python program to add two numbers without using arithmetic operators
Suggested for you
For loop in Java language
While loop in Java language
Operator in Java language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…