Showing posts with label decimal-to-binary. Show all posts
Showing posts with label decimal-to-binary. Show all posts

Tuesday, August 4, 2020

Write a program to input a number in Decimal and convert it Binary form?(9 in decimal then 1001 in binary, divide by 2)

70)  Write a program to input a number in Decimal and convert it Binary form?(9 in decimal then 1001 in binary, divide by 2)


import java.util.*;
public class Decimal_to_binary
{
    public static void main(String args[ ])
   {
           Scanner sc = new Scanner (System.in);
           System.out.println("Enter a number");
           int b = sc.nextInt();
           int a = b;
           String c = "";
           while(a>=1)
           {
               c=c+(a%2);
               a=a/2;
           }
           int k = c.length();
           System.out.print("Entered number in BINARY = ");
           for(int i = k-1;i>=0;i--)
           {
                System.out.print(c.charAt(i));
           }
   }
}


Output : 




 jhvghvgk ;nkjn ,nkjhj