Skip to content

Instantly share code, notes, and snippets.

@sdmg15
Created October 22, 2016 17:36
Show Gist options
  • Save sdmg15/f36db9e55a22d288ee79921373dc91bc to your computer and use it in GitHub Desktop.
Save sdmg15/f36db9e55a22d288ee79921373dc91bc to your computer and use it in GitHub Desktop.
Simple decimal to binary converter
import java.util.Scanner;
public class sdz {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nbrTodiv, modDiv,tmp;
String result ="";
System.out.println("Entrez le nombre à convertir ");
nbrTodiv = sc.nextInt();
tmp = nbrTodiv;
while (nbrTodiv >= 1){
modDiv = nbrTodiv%2;
result = String.valueOf(modDiv)+result;
nbrTodiv = nbrTodiv/2;
}
switch(result.length()) {
case 1:
result = "000"+ result;
break;
case 2:
result = "00"+result;
break;
case 3:
result = "0"+result;
break;
}
System.out.println("La conversion en binaire de " + tmp + " donner " + result );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment