Skip to content

Instantly share code, notes, and snippets.

@iamaamir
Created February 21, 2018 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamaamir/d6642de0da1cdc93cb2386867232c91a to your computer and use it in GitHub Desktop.
Save iamaamir/d6642de0da1cdc93cb2386867232c91a to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class DecimalToBinary {
public static void main(String[] args) {
new DecimalToBinary().run();
}
public void toDecimal(int number){
int binary[] = new int[number];
int index = 0;
while(number > 0){
binary[index++] = number%2;
number = number/2;
}
for(int i = index-1;i >= 0;i--){
System.out.print(binary[i]);
}
}
private void run() {
toDecimal(input("Enter a Decimal: "));
}
public int input(String msg) {
System.out.println(msg);
return new Scanner(System.in).nextInt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment