addition

Program to sum of two integer using without + operator in Java

Program to sum of two integer using without + operator in Java

In this article, we will discuss the concept of the Program to sum of two integer using without + operator in Java.

In this post, we are going to learn how to  write a program to find the sum of two numbers using without plus operator in Java programming language

Code to find the sum of  two numbers 

Java code to Add two integer

Sum of two integer using minus operator

The program allows the user to enter two integers and then calculates the sum of given  numbers using minus operator in Java language

Program 1

//sum of two integer numbers using without + in Java
import java.util.*;
public class SumNumWithoutPlus{
public static void main(String args[]){
    int num1,num2;
    Scanner sc=new Scanner(System.in);
    //scanner class for input
System.out.println("Please Enter the first integer number ");
num1=sc.nextInt();//get input from user for num1
System.out.println("Please Enter the second integer number ");
num2=sc.nextInt();//get input from user for num2
int result =num1-(-num2);//calculate the sum
System.out.println("Sum of two numbers "+num1+" and "+num2+" is: "+result);
}
}

When the above code is executed, it produces the following result

Please Enter the first integer number
225
Please Enter the second integer number
340
Sum of two numbers 225 and 340 is: 565

 

Sum of two integer  using Bitwise operator

The program allows the user to enter two integers and then calculates the sum of given  numbers using Bitwise operator in Java language

Program 2

//sum of two integer numbers using bitwise in Java
import java.util.*;
public class SumNumBitwise{
public static void main(String args[]){
    int x,y;
    Scanner sc=new Scanner(System.in);
    //scanner class for input
System.out.println("Please Enter the first integer number ");
int num1=sc.nextInt();//get input from user for num1
System.out.println("Please Enter the second integer number ");
int num2=sc.nextInt();//get input from user for num2
x=num1;
y=num2;
while(num2!=0){
    int brw =num1&num2;
    num1=num1^num2;
    num2=brw<<1;
}
System.out.println("Sum of two numbers "+x+" and "+y+" is: "+num1);
}
}

When the above code is executed, it produces the following result

Please Enter the first integer number
123
Please Enter the second integer number
234
Sum of two numbers 123 and 234 is:  357

 

Sum of two integer  using Bitwise with method

The program allows the user to enter two integers and then calculates the sum of given  numbers using Bitwise operator with method in Java language

Program 3

//sum of two integer numbers using bitwise in Java
import java.util.*;
public class SumNumwithoutPlusmethod{
public static void main(String args[]){
    int x,y;//variable declaration
    Scanner sc=new Scanner(System.in);
    //scanner class for input
System.out.println("Please Enter the first integer number ");
int num1=sc.nextInt();//get input from user for num1
System.out.println("Please Enter the second integer number ");
int num2=sc.nextInt();//get input from user for num1
x=num1;//Assign the value of num1 to x
y=num2;//Assign the value of num2 to y
subNum(num1,num2);//Calling the method
}
static void subNum(int num1,int num2){//method definition
while(num2!=0){//calculate sum of two numbers using bitwise operator
    int brw =num1&num2;
    num1=num1^num2;
    num2=brw<<1;
    
}
System.out.println("Sum of two numbers is: "+num1);
//display output on the screen

}
}

When the above code is executed, it produces the following result

Please Enter the first integer number
125
Please Enter the second integer number
456
Sum of two numbers 125 and 456 is: 581

 

Sum of two integer using increment, decrement operator

The program allows the user to enter two integers and then calculates the sum of given  numbers using increment and decrement operator in Java language.

Program 4

//Sum two integer numbers using bitwise in Java
import java.util.*;
public class SumNum_WithoutPlus2{
public static void main(String args[]){
    int x,y;
    Scanner sc=new Scanner(System.in);
    //scanner class for input
System.out.println("Please Enter the first integer number ");
int num1=sc.nextInt();
System.out.println("Please Enter the second integer number ");
int num2=sc.nextInt();
System.out.println("Sum of two numbers is: "+add(num1,num2));
}
static int add(int num1,int num2){
while(num1>0){
  num2++;
  num1--;
}
while(num1<0){
  num2--;
  num1++;
}
return num2;
}
}

When the above code is executed, it produces the following result

Please Enter the first integer number
125
Please Enter the second integer number
250
Sum of two numbers 125 and 250 is: 375

 

Sum of two integer using for loop

The program allows the user to enter two integers and then calculates the sum of given  numbers using for loop in Java language

Program 5

//Sum two integer numbers using bitwise in Java
import java.util.*;
public class SumNum_WithoutPlus3{
public static void main(String args[]){
    int x,y;
    Scanner sc=new Scanner(System.in);
    //scanner class for input
System.out.println("Please Enter the first integer number ");
int num1=sc.nextInt();
System.out.println("Please Enter the second integer number ");
int num2=sc.nextInt();
System.out.println("Sum of two numbers is: "+add(num1,num2));
}//Calling the method
static int add(int num1,int num2){//method definition
int i;
for(i=0; i<num2; i++){
   num1++;
}
return num1;
}
}

When the above code is executed, it produces the following result

Please Enter the first integer number
1500
Please Enter the second integer number
2200
Sum of two numbers 1500 and 2200 is: 3700

 

Suggested for you

Operator in Java language

Data type and variable in Java language

Class and main method in Java language

If statements in Java language

Method in Java language

 

 

Similar post

Subtract two numbers in Java language

Subtract two numbers using method in Java language

Subtract two numbers using recursion in Java language

 

Find sum of two numbers in Java language

Find sum of two numbers  using method in Java language

Find sum of two numbers in Java using recursion

Find sum of two numbers in Java without Arithmetic operator

Find sum of natural numbers in Java language

Find sum of natural numbers in Java language using recursion

Python Example to sum of two integer using Bitwise operator
Sum of two integer using without + operator in C
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