Decimal Number to Binary Number

Java Program to find Binary Number of the Decimal Number entered by user.



package loops;
import java.util.Scanner;

public class DecimalToBinary
{
public static void main(String[] args) 
{
//Program to Convert the user Decimal Number to Binary Number
Scanner input=new Scanner(System.in);
System.out.println("Enter a number:");
int n=input.nextInt(); //n=200
System.out.print("Binary Number of "+n+" is ");
int temp=n;//temp=200
for(int i=1073741824;i>0;i/=2)//i=0 //n=36
{
if(i<=temp)//i=8
{
System.out.print("1");
temp-=i;//temp=0
}
else if(i<n)
System.out.print("0");
}
}
}
 

Comments

Popular posts from this blog

Power

Palindrome Number