Search Tutorials

Tuesday, 23 April 2013

Java code to convert Decimal into Binary

Java program to convert Decimal into Binary.

import java.io.*;
class Decimal2binary
{
 public static void main(String args[]) throws IOException
 {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 Decimal2binary call = new Decimal2binary();
 System.out.print("Enter a decimal number : ");
 int n = Integer.parseInt(br.readLine());
 System.out.print("Binary Equivalent : ");
 call.dectobin(n);
 System.out.print("\n");
 }
 void dectobin(int n)
 {
 int c;
 if(n!=0)
 {
  c=n%2;
  dectobin(n/2);
  System.out.print(c);
 }
 }
}

ALGORITHM:-

1. Start

2. Accept a decimal number from the user.

3. Using recursion, convert it to binary.

4. Print the Binary Equivalent.

5. End

OUTPUT:-

Java code to convert Decimal into Binary

Enter a decimal number : 25

Binary Equivalent : 11001

Related Programs:-

Convert Decimal to Octal

Print the Factorial of a number using recursion

Print the possible combination of a string

Generate Fibonacci series using recursion

Find the HCF & LCM of two numbers using recursion

No comments:

Post a Comment

Back to Top