This is a Java Program to Convert a Number Decimal System to Binary System using Recursion.
Enter any number as an input. Now we make a new method named binary with return type string that gives us the desired result with the help of modulus operation.
Here is the source code of the Java Program to Convert a Number Decimal System to Binary System 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 Convert
{
public static void main(String[] args)
{
int n;
String x;
Scanner s = new Scanner(System.in);
System.out.print("Enter any decimal number:");
n = s.nextInt();
Convert obj = new Convert();
x = obj.binary(n);
System.out.println("Binary number:"+x);
}
String binary(int y)
{
int a;
if(y > 0)
{
a = y % 2;
return (binary(y / 2) + "" +a);
}
return "";
}
}
Output:
$ javac Convert.java $ java Convert Enter any decimal number:25 Binary number:11001
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice Programming MCQs
- Check Programming Books
- Practice BCA MCQs
- Apply for Java Internship
- Check Java Books