Tuesday, July 25, 2023

DecimalToBinary

 

import java.util.Scanner; 

import java.lang.Math; 

public class Main

{

    public static int convertDecimalToBinary(int dec) {

   

      int rem,b1=0;

      int rev = 1;

      while (dec > 0) {

         rem = dec % 2;

         dec = dec / 2;

         b1 = b1 + rem * rev;

         rev = rev * 10;

      }

    return b1;

  }

public static void main(String args[])    

{    

 

 

    Scanner in = new Scanner(System.in); 

System.out.print("Enter a number : "); 

 

int num = in.nextInt(); 

 

    int decimal = convertDecimalToBinary(num);

    System.out.println("Binary to Decimal");

    System.out.println(num + " = " + decimal);

  }

 

 

}

No comments:

Post a Comment