Converting a decimal number to a binary number

sravan953 0 Tallied Votes 228 Views Share

This program converts any number that you enter to a binary number. For more information on binary numbers, visit:

http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary

:)

import java.io.*;
class decimal_binary
{
static void convert()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter a number: ");
        int n=Integer.parseInt(br.readLine());
        String s="";
        for(;n>0;)
            {
                int q=n%2;
                s=Integer.toString(q)+s;
                n/=2;
            }
            System.out.println(s);
        }
    }