Decimal to Binary in JAVA programming



import java.util.Scanner;

public class DecimalToBinary {

    public static void main(String[] args) {
        Scanner myScanner=new Scanner(System.in);
       
        int n;
        int[] arr=new int[100];
        System.out.print("Enter a Decimal number : ");
        n=myScanner.nextInt();
        int i=0;
        while (n!=0) {
            arr[i]=n%2;
            n=n/2;
            i++;
        }
        System.out.print("Binary value is :");
        for (int j =i-1; j>=0; j--) {
            System.out.print(arr[j]);
        }
        System.out.println();
    }

}
//Enter a Decimal number : 567
//Binary value is :1000110111

No comments:

Post a Comment