addition

Java program to add two numbers without using arithmetic operators

Java program to add two numbers without using arithmetic operators

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

Addition

Java program to add two numbers

Using for loop – program 1

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

  • The user inputs the first number
  • Store  the number in variable num1
  • The user inputs the second number
  • Store  the number in variable num2
  • for loop” used for the addition of two numbers.
  • Display the sum of the numbers on the screen.

 

Using while loop – program 2

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

  • The user inputs the first number
  • Store  the number in variable num1
  • The user inputs the second number
  • Store  the number in variable num2
  • while loop” used for the addition of two numbers.
  • Display the sum of the numbers on the screen.

 

Using for loop inside the method – program 3

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

  • The user inputs the first number
  • Store  the number in variable n1
  • The user inputs the second number
  • Store  the number in variable n2
  • Create a method with  “for loop” to find the sum of two numbers.
  • Call the method inside the main method.
  • Display the sum of the number

 

 

Using while loop inside the method – program 4

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

  • The user inputs the first number
  • Store  the number in variable n1
  • The user inputs the second number
  • Store  the number in variable n2
  • Create a method with  “while loop” to find the sum of two numbers.
  • Call the fmethod  inside the main method.
  • Display the sum of the number

 

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

Java program to sum of two numbers

C program to sum of two numbers

C++ program to sum of two numbers

Python program to sum of two numbers

C++ program to find the sum  of two numbers  using recursion

C program to find the sum  of two numbers  using recursion

Java program to find the sum  of two numbers  using recursion

Python program to find the sum  of two numbers  using recursion

 

Suggested for you

For loop in Java language

While loop in Java language

The method in Java language

Operator in Java language

variable in Java language

 

 

 

 

C++ program to add two numbers without using arithmetic operators
Python program to find sum of two numbers without using arithmetic operators
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago