import java.util.Scanner;
public class DecimalToBinary {
public static void main(String [] args){
//Taking input from user
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer:");
int x=input.nextInt();
//Taking be a string to integer and goes to method
System.out.println(""+toBinary(x));
}
public static String toBinary(int n){
//This program calculates just positive integers
if(n<=-1){
System.out.println("Please write positive integer");
return null;
}
//Necessary calculation to convert decimal to binary
String binary ="";
while(n>0){
int remaining = n%2;
binary=remaining+binary;
n/=2;
}
binary = n+binary;
return binary;
}
}
Hiç yorum yok:
Yorum Gönder