Prerequisite: Recursion in Java
In the previous article, we have discussed about Java Program to Reverse an Array by Using Recursion
In this program we are going to see how to convert decimal to binary using Recursion by Java programming language.
Java Program to Convert Decimal to Binary Using Recursion
Lets assume there is a decimal number A = 786
So the binary of 786 = 1100010010
Now let’s see different ways to convert decimal to binary by using Recursion.
Method-1: Java Program to Convert Decimal to Binary By Using Static Input and Recursion
Approach:
- Declare and initialize an integer variable say ‘
dec
’ - Call a user defined method
calculateBin()
and pass the ‘dec
’ as parameter. - Inside the user defined method, check if the decimal number is zero or not. If the decimal number is 0 then return 0 else call the same method as “
(dec%2) + (10*(calculateBin(dec/2)))
” and return the value to the main method. - Now the value of the user defined method
calculateBin()
is stored in an integer variable say ‘bin
’. - Print the value of binary number.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { //declare and initialize an integer variable dec int dec = 786; //call the method and store the value inside an integer variable say ‘bin’ int bin = calculateBin(dec); //print the result System.out.println("The binary form of the decimal number "+dec+" is: "+bin); } //calculateBin() method to convert decimal to binary static int calculateBin(int dec) { if (dec == 0) return 0; else return (dec%2) + (10*(calculateBin(dec/2))); } }
Output: The binary form of the decimal number 786 is: 1100010010
Method-2: Java Program to Convert Decimal to Binary Using Recursion By Using User Input and Recursion
Approach:
- Create a scanner class object.
- Declare an integer variable say ‘
dec
’ - Prompt the user to enter a decimal number.
- Call a user defined method
calculateBin()
and pass the ‘dec
’ as parameter. - Inside the user defined method, check if the decimal number is zero or not. If the decimal number is 0 then return 0 else call the same method as “
(dec%2) + (10*(calculateBin(dec/2)))
” and return the value to the main method. - Now the value of the user defined method
calculateBin()
is stored in an integer variable say ‘bin
’. - Print the value of binary number.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { // create a scanner class Scanner s = new Scanner(System.in); System.out.println("Enter a decimal number:"); //declare an integer variable ‘dec’and initialize it by user input using scanner class. int dec = s.nextInt(); //define the method and store the value inside an integer variable say ‘bin’ int bin = calculateBin(dec); //print the result System.out.println("The binary form of the decimal number "+dec+" is: "+bin); } static int calculateBin(int dec) { if (dec == 0) return 0; else return (dec%2) + (10*(calculateBin(dec/2))); } }
Output: Enter a decimal number: 17 The binary form of the decimal number 17 is: 10001
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Related Java Programs: