Question

Write a java program to convert a decimal number to binary number. Provide the Big O...

Write a java program to convert a decimal number to binary number. Provide the Big O analysis

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//TestCode.java
import java.util.Scanner;
public class TestCode {
  public static String dec2Bin(int value) {
    if (value == 0) {
      return "";
    } else {
      return dec2Bin(value / 2) + "" + (value % 2);
    }
  }

  public static void main(String args[]) {
    int num;
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter decimal number: ");
    num = sc.nextInt();
    System.out.println(dec2Bin(num));
  }
}

Output:

Enter decimal number: 5 101 Process finished with exit code 0

\color{blue}\underline{Time \;complexity\;:}

Time complexity = =O(Number of digits in the resultant binary)

Please upvote the solution if it helped. Thanks!

Note: Please comment below if you have any doubts

Add a comment
Know the answer?
Add Answer to:
Write a java program to convert a decimal number to binary number. Provide the Big O...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT