In this article, we will discuss the concept of the Java Example to subtract two integer without using minus operator
In this post, we are going to learn how to write a program to find the subtraction of two numbers using with out minus operator in Java programming language
The program use to calculate subtraction of given two integer numbers without minus operator in Java language
Program 1
import java.util.Scanner; public class SubTwoNumwithoutminus{ public static void main(String args[]){ int sub; Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the value to num1: "); int num1=scan.nextInt(); System.out.print("Enter the value to num2: "); int num2=scan.nextInt(); sub=num1+~num2+1; System.out.print("subtraction of "+num1+" -"+num2+" :"+sub); } }
When the above code is executed, it produces the following result
Enter te value to num1: 567 Enter te value to num2: 234 subtraction of 567 -234 :333
Methods
The program allow the user to enter two integers and then calculates subtraction of given numbers using without minus operator in Java language
Program 2
import java.util.Scanner; public class SubTwoNumwithoutminus1{ public static void main(String args[]){ int sub,num1,num2; Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the value to num1: "); num1=scan.nextInt(); System.out.print("Enter the value to num2: "); num2=scan.nextInt(); System.out.print("subtraction of "+num1+"-"+num2+" :"+subtract(num1,num2)); }//Calling the method static int subtract(int num1,int num2)//method definition { if(num2==0) return num1; return subtract(num1^num2,(~num1 & num2)<<1); } }
When the above code is executed, it produces the following result
case 1
Enter the value for num1=60 Enter the value for num2=45 subtraction of 60-45 =15
case 2
Enter the value for num1=50 Enter the value for num1=227 subtraction of 50-227 :-177
Methods
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
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
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…