Decimal to Binary

// Decimal to Binary
public class Dec2Bin
{
public static void main(long n)
{
long temp=n;
long sum=0,rem,p=0;
while(n>0)
{
rem=n%2;
sum=sum + (long)(rem*(Math.pow(10,p)));
n=n/2;
p++;
}

System.out.println(“Binary of “+temp+” = “+sum);
}
}

Advertisements
Comments
  1. rekha joshi says:

    hi neel
    you are doing a great job…very nice job..
    i am new to java i referred your blog…..mind blowing knowledge of you.
    i have one doubt here in that question???
    In java as an main() method argument you can pass long value in above program……it is valid???
    I am java beginner and new idea i m gaining?
    so its valid, bcoz as per my knowledge java only taken String argument as input….

    Plz reply
    thanks
    rekha

    • Neel says:

      yes you can
      the point is that we use an environment for taking inputs
      like the one i have used is BLUE-J and there will be absolutely no issues if you use any of the primitive types,
      in fact all the teachers those who teach in schools have a bad habit of writing void main(String args[]) even if they are not using the String arguments,
      this causes a lot of confusion among students and they land up writing String args[] in every program even if the program is to display
      “MY FIRST JAVA PROGRAM”
      Anyways, you obviously can.
      But practically we keep the main() method argument list empty coz we use java.io or java.util package
      to take interactive inputs from the terminal window…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s