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

subtract two integer without minus

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

  1. Create two variables to store two numbers as input provided by the user: num1,num2;
  2. Create a variable to store the subtraction of these numbers: sub
  3. Request the user to enter first input for store variable num1
  4. Using input scanner  to store the first input value in num1
  5. Request the user to enter second input for store variable num2
  6. Using input scanner   to store the second input value in num2
  7. Calculate the subtraction of two number using without minus operator
  8. 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

  1. Create two variables to store two numbers as input provided by the user: num1,num2;
  2. Create a variable to store the subtraction of these numbers: sub
  3. Request the user to enter first input to store variable num1
  4. Using input scanner to store the first input value in num1
  5. Request the user to enter second input to store variable num2
  6. Using input scanner to store the second input value in num2
  7. Define a method  to find subtraction using without minus operator
  8. Calculate the subtraction of two number through calling the method
  9. 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

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

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

 

 

Program in c++ to multiply two floating-point numbers using function
Python Example to subtract two integer without using minus operator
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