15 June, 2012

Convert decimal to binary

Decimal to Binary



import java.util.*;
public class NumberSystem
{
    public static void main(String[] args)
    {
        Scanner s = new Scanner (System.in);
        System.out.printf("\nEnter The Value in Decimal No.-: ");
        int n = s.nextInt();
        int c = 2;
        String str = " ";

        for ( ; n>0; )
        {
            int r = n%c;
            if (r<10)
            str = " "+r+str;
            n = n/c;
        }

    System.out.printf("\nThe Converted Number is  %s\n",str);
    }
}

No comments:

Post a Comment

Thank You !