Recent Posts

Responsive Ads Here

Wednesday, 30 September 2015

How to convert Decimal to binary in Java

Method -2 :
package ensuretoday.blogspot.in;

import java.util.Scanner;

public class DecimalToBinaryConverstion {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
System.out.println("Enter a Decimal Number");
int number=sc.nextInt();
int c,r=0;
String str="";
c=number;
if(number==0)
{
System.out.println(number);
}
else
{
while(number!=0)
{
c=number/2;
r=number%2;
str=str+r;
number=c;
}
}
    String[] b=str.split("");
    for(int i=b.length-1;i>=0;i--)
    {
    System.out.println(b[i]);
    }
    sc.close();
}

}

Out put :-








No comments:

Post a Comment