This is a Java Program to Print Binary Equivalent of an Integer using Recursion.
Enter any integer number as an input. After that we pass the given number to the other function where by using modulo operator along with recursion we get the reverse of a number as an output.
Here is the source code of the Java Program to Print Binary Equivalent of an Integer using Recursion. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Binary_Recursion
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
n = s.nextInt();
Binary_Recursion obj = new Binary_Recursion();
String m = obj.Binary(n);
System.out.println("Answer:"+m);
}
String Binary(int x)
{
if(x > 0)
{
int a = x % 2;
x = x / 2;
return a + "" + Binary(x);
}
return "";
}
}
Output:
$ javac Binary_Recursion.java $ java Binary_Recursion Enter the number:19 Answer:11001
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice BCA MCQs
- Check Programming Books
- Apply for Java Internship
- Practice Programming MCQs
- Check Java Books