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 : 




No comments:

Post a Comment

if you have any doubts, Please let me know.
Your Truly
Shashank Porwal

 jhvghvgk ;nkjn ,nkjhj