Java >> Tutoriel Java >  >> Java

Programme JAVA pour convertir décimal en binaire

Dans ce didacticiel, vous apprendrez à convertir un décimal en binaire en JAVA en utilisant custom et toBinaryString() méthode et une méthode personnalisée.

Prenons un exemple,

  • Nombre décimal : 23
  • Numéro binaire : 10111

  • Nombre décimal : 19
  • Numéro binaire : 10011

Utilisation de la méthode personnalisée

Voyons comment convertir un décimal en binaire en JAVA à l'aide d'une méthode personnalisée.

Exemple :

//Java program to get the the binary notation of a decimal number

//Custom Method

//Importing the Scanner Class of Util Package
import java.util.Scanner;

//Main Class of the Program
public class Main
{
    //Main Method of the program
    public static void main(String[] args) {
        //Declaration of variables
        int numberModule,decimalNumber;
        String binaryNumber = "";
        //Creating the object of Scanner Class
        Scanner input = new Scanner(System.in);
        //Telling user what to enter
        System.out.print("Enter a Decimal Number : ");
        //Taking input from user
        decimalNumber = input.nextInt();
        //Loop till number is greater than 0
        while(decimalNumber > 0){
          //Finding the modulation of the entered number with 2
          numberModule = decimalNumber % 2;
          //Adding the modulation result in a variable
          binaryNumber = numberModule + "" + binaryNumber;
          //removing the last digit from entered number
          decimalNumber = decimalNumber / 2;
        }
        //Printing the result
        System.out.println("Binary Notation : "+binaryNumber);
    }
}

Sortie :

Enter a Decimal Number : 20
Binary Notation : 10100 

Utilisation de la méthode toBinaryString()

Le toBinaryString() La méthode en Java renvoie la représentation sous forme de chaîne de l'argument entier que vous lui transmettez. Cette chaîne est un entier non signé en base 2.

Exemple :

//Java program to get the the binary notation of a decimal number

//toBinaryString()

//Importing the Scanner Class of Util Package
import java.util.Scanner;

//Main Class of the Program
public class Main
{
    //Main Method of the program
    public static void main(String[] args) {
        //Declaration of variables
        int numberModule,decimalNumber;
        //Creating the object of Scanner Class
        Scanner input = new Scanner(System.in);
        //Telling user what to enter
        System.out.print("Enter a Decimal Number : ");
        //Taking input from user
        decimalNumber = input.nextInt();
        //Taking returned value from the toBinaryString Method in a variable
        String binaryNumber = Integer.toBinaryString(decimalNumber);
        //Printing the result
        System.out.println("Binary Notation : "+binaryNumber);
    }
}

Sortie :

Enter a Decimal Number : 21
Binary Notation : 10101 

Conclusion

Les deux méthodes décrites ci-dessus vous aideront à convertir un nombre décimal en binaire en Java. La fonction personnalisée fonctionne bien, mais le toBinaryString() méthode est plus rapide et efficace.


Balise Java