Tuesday, 16 January 2018

Write a Java program to convert Decimal Number into Binary Example

package com.tutorial4u;

import java.util.Scanner;

public class DecimaltoBinary {
       public static void main(String[] args) {
             int decimal;
             System.out.print("Enter any decimal number : ");
             Scanner sc = new Scanner(System.in);
             decimal = sc.nextInt();
             System.out.println("Decimal "+decimal+" = Binary " + decimalToBinaryConverter(decimal));
       }

       private static String decimalToBinaryConverter(int decimal) {
              String binary = "";
              if (decimal == 0) {
                   return "0";
               }
               while (decimal > 0) {
                  binary = (decimal % 2) + binary;
                  decimal = decimal / 2;
               }
             return binary;
       }
}



Output : 

Enter any decimal number : 11
Decimal 11 = Binary 1011

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...