Created
October 22, 2016 17:36
-
-
Save sdmg15/f36db9e55a22d288ee79921373dc91bc to your computer and use it in GitHub Desktop.
Simple decimal to binary converter
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 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