Created
February 4, 2018 07:13
-
-
Save TranNgocMinh/78d084f16a73ce4d43adfe14aa17dc0a 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 Program { | |
public static void main(String[] args) { | |
long number; | |
Scanner scanner = new Scanner( System.in ); | |
System.out.print( "Nhap so he thap phan: " ); | |
number = Long.parseLong(scanner.nextLine() ); | |
System.out.println("Dang nhi phan la:" + binary(number)); | |
} | |
public static long binary(long number){ | |
long base = 1, binary_val = 0, rem; | |
while (number > 0) | |
{ | |
rem = number % 2; | |
binary_val = binary_val + rem * base; | |
number = number / 2; | |
base = base * 10; | |
} | |
return binary_val; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment