Skip to content

Instantly share code, notes, and snippets.

@BT-ICD
Created April 28, 2021 17:39
Show Gist options
  • Save BT-ICD/f3aa67b9176f3946d5aa5bc3bc7a68c4 to your computer and use it in GitHub Desktop.
Save BT-ICD/f3aa67b9176f3946d5aa5bc3bc7a68c4 to your computer and use it in GitHub Desktop.
Conversion from decimal to binary
/**
* Example: Conversion from decimal to binary
* Reference: https://www.rapidtables.com/convert/number/decimal-to-binary.html
* Sample Data:
* 11 - 1011
* 12 - 1100
* 13 - 1101
**/
public class DecimalToBinaryDemo {
public static void main(String[] args) {
int num=13, rem;
StringBuilder ans= new StringBuilder();
while(num>0){
rem = num%2;
num=num/2;
ans.append(rem);
}
ans.reverse();
System.out.println(ans);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment