Created
February 21, 2018 20:29
-
-
Save iamaamir/d6642de0da1cdc93cb2386867232c91a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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