Java Example to subtract two integer without using minus operator
- Home
- Calculations
- Java Example to subtract two integer without using minus operator
- On
- By
- 0 Comment
- Categories: Calculations, subtraction
Java Example to subtract two integer without using minus operator
Java Example to subtract two integer without using minus operator
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
Code to find the subtraction of two numbers
Subtract two integer using without minus
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
- Create two variables to store two numbers as input provided by the user: num1,num2;
- Create a variable to store the subtraction of these numbers: sub
- Request the user to enter first input for store variable num1
- Using input scanner to store the first input value in num1
- Request the user to enter second input for store variable num2
- Using input scanner to store the second input value in num2
- Calculate the subtraction of two number using without minus operator
- Finally, display result on the screen- the subtraction of both numbers num1 and num2
Subtract two integer using without minus – using method
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
- Create two variables to store two numbers as input provided by the user: num1,num2;
- Create a variable to store the subtraction of these numbers: sub
- Request the user to enter first input to store variable num1
- Using input scanner to store the first input value in num1
- Request the user to enter second input to store variable num2
- Using input scanner to store the second input value in num2
- Define a method to find subtraction using without minus operator
- Calculate the subtraction of two number through calling the method
- Finally, display result on the screen- the subtraction of both numbers num1, and num2
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