Calculations

Use of Java program to subtraction of two numbers using recursion

Use of Java program to subtraction of two numbers using the recursion

In this tutorial, we will discuss the Use of Java program to subtraction of two numbers using the recursion

In this topic, we are going to learn how to subtract two numbers using the recusive function in Java language

already we are learned the same concept using the operator

if you want to remember click here, Java program to subtract two numbers

subtraction of two numbers using recursion

Program to subtraction of two numbers using recursion

The program allows to enter two integer numbers and then it calculates the subtraction of the given numbers using recursive function

Program 1

import java.util.Scanner;
public class SubTwoNumbersRec{
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();

//reads the numbers from the user
//input from keyword
sub=subtract(num1,num2);//call te method
System.out.print("Subtraction of two numbers: "+sub);
}
static int subtract(int num1, int num2)//method definition
{
    if(num2==0)
        return num1;
    else
        return subtract((num1-1),(num2-1));
    
}
}

When the above code isexecuted, it produces the folowing result

Enter the value to num1: 56
Enter the value to num2: 20
Subtraction of two numbers:26

 

  • Declare three int type variables num1,num2,sub. num1,num2 are used to received input from the user whereas the sub is used to assign the output
  • Receive input from the user for num1,num2 to perform subtraction
  • Define a recursive method to subtract two numbers
  • When the method is called, two numbers will be passed as argument subsequently the subtract of two numbers will be found
  • Then, assign te output to the variable sub
  • display the result on the screen

 

Suggetsed for you

Method in Java language

Class and main method

Data type and variable

 

Similar post

Subtract two numbers in Java language

Subtract two numbers in C language

Subtract two numbers in C++ language

Subtract two numbers in Python language

 

Subtract two numbers in Java language using method

Subtract two numbers in C language using function

Subtract two numbers in C++ language using function

Subtract two numbers in Python language using function

 

 

Java program to subtract two number using method
Use of C++ program to subtraction of two numbers using recursion
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