Java program to divide two numbers using recursion
- Home
- Calculations
- Java program to divide two numbers using recursion
- On
- By
- 0 Comment
- Categories: Calculations, recursion
Java program to divide two numbers using recursion
Java program to divide two numbers using recursion
In this tutorial, we will discuss the concept of Java program to divide two numbers using recursion
In this topic, we are going to learn how to divide two numbers using the recursive method in Java language
What is division
The division is a method of splitting a group of things into equal parts. The division is an arithmetic operation inverse of multiplication
It is one of the four basic operation of arithmetic calculation others being addition,subtraction,multiplication
The division of two natural numbers means it is the operation of calculating the number of times one number is contained within one another
Java exercise to Divide two numbers
Program to division of two numbers
The program calculates the division of the given two numbers using Java recursive method
Program 1
public class FindDivisionRecWithoutUserinput{ public static void main(String args[]){//main method int num1=100,num2=5;//variable declaration and initialization int result=divisionNum(num1,num2);//calling the method System.out.println("Division of "+num1+" and "+num2+":"+result); } static int divisionNum(int x,int y){//recursive method if (y==0) { return 0; } else if (x-y==0){ return 1; } else if (x<y){ return 0; } else{ return (1+divisionNum(x-y,y)); } } }
When the above code is executed, it produces the following result
Division of 100 and 5 :20
Program to division of two numbers – Entered by user
Program 2
The program allows the user to enter two integer numbers and then it calculates the division of the given numbers using the recursive method in Java language
import java.util.Scanner; public class FindDivisionRecursion{ public static void main(String args[]){//main method 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: "); //Ask input from the user for num1 int num1=scan.nextInt(); //Reading the value entered by user System.out.print("Enter the value to num2: "); //Ask input from the user for num1 int num2=scan.nextInt(); //Reading the value entered by user int result=divisionNum(num1,num2);//calling the method System.out.println("Division of "+num1+" and "+num2+":"+result); } static int divisionNum(int x,int y){//recursive method if (y==0) { return 0; } else if (x-y==0){ return 1; } else if (x<y){ return 0; } else{ return (1+divisionNum(x-y,y)); } } }
When the above code is executed, it produces the following result
Enter the value to num1: 300 Enter the value to num2: 12 Division of 300 and 12 :25
Suggested for you
Operator in Java
Data types and variable in Java
Class and main method in Java language
Similar post
Find product of two numbers using method in Java
Find product of two numbers using recursion in Java
Multiply two numbers in C language
Multiply two numbers in C++ language
Multiply two numbers in Python language
C Program to Divide of two integer numbers
Java Program to Divide of two integer numbers
C++ Program to Divide of two integer numbers
Python Program to Divide of two integer numbers