-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecimalToBinary.java
More file actions
38 lines (37 loc) · 1.24 KB
/
DecimalToBinary.java
File metadata and controls
38 lines (37 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Program: Convert Decimal Number to Binary Using Recursion
// Topic: Recursion and Number Conversion
// Description: Reads a decimal number from user input and recursively converts it into its binary representation.
// The helper method `DectoBinHelp()` divides the number by 2 at each step and appends remainders recursively.
// Demonstrates recursive function calls, base conditions, and string concatenation for binary conversion.
package recursion;
import java.util.*;
/**
*
* @author Samim
*/
public class DecimalToBinary
{
private static String DectoBin(int decimalNumber) {
if (decimalNumber == 0) {
return "0";
} else {
return DectoBinHelp(decimalNumber);
}
}
private static String DectoBinHelp(int decimalNumber) {
if (decimalNumber == 0) {
return "";
} else {
int remainder = decimalNumber % 2;
return DectoBinHelp(decimalNumber / 2) + remainder;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num;
System.out.println("Enter Num :");
num=sc.nextInt();
System.out.println(DectoBin(num));
}
}