Skip to content

Instantly share code, notes, and snippets.

@TranNgocMinh
Created February 4, 2018 07:13
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 TranNgocMinh/78d084f16a73ce4d43adfe14aa17dc0a to your computer and use it in GitHub Desktop.
Save TranNgocMinh/78d084f16a73ce4d43adfe14aa17dc0a to your computer and use it in GitHub Desktop.
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