Converting Decimal to Binary Java

Converting Decimal to Binary Java

Your binaryForm method is getting caught in an infinite recursion, you need to return if number <= 1:

import java.util.Scanner;

public class ReversedBinary {

public static void main(String[] args) {
int number;

Scanner in = new Scanner(System.in);

System.out.println("Enter a positive integer");
number = in.nextInt();

if (number < 0) {
System.out.println("Error: Not a positive integer");
} else {

System.out.print("Convert to binary is:");
//System.out.print(binaryform(number));
printBinaryform(number);
}
}

private static void printBinaryform(int number) {
int remainder;

if (number <= 1) {
System.out.print(number);
return; // KICK OUT OF THE RECURSION
}

remainder = number % 2;
printBinaryform(number >> 1);
System.out.print(remainder);
}
}

Converting decimal to binary in Java

Integer.toBinaryString(int) should do the trick !

And by the way, correct your syntax, if you're using Eclipse I'm sure he's complaining about a lot of error.

Working code :

public class NumberConverter {
public static void main(String[] args) {
int i = Integer.parseInt(args[0]);
toBinary(i);
}

public static void toBinary(int int1){
System.out.println(int1 + " in binary is");
System.out.println(Integer.toBinaryString(int1));
}
}

Convert Decimal Number to Binary without using Array

I found following "direct" issues with your code:

  1. You are not reversing the final number. In the manual way of converting DEC->BIN, we reverse the final representation. Hint: Dry run for input 11
  2. You are doing num += rem; num *= 10. The order is wrong. You should be multiplying it before you add the remainder. eg. Your code will output 1010 for 5, instead of 101.
  3. By your method, you are trying to represent decimal number into its binary representation as int, which limits your input to 2047, as 2048 and above need 11+ digits for their binary representation and you can't have that in int. You should use String if you don't want to use array. Also, reversing would be easier.

something like:

import java.util.Scanner;

public class DecToBin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int dec = 0, num = 0;

System.out.print("Enter A Decimal Number: ");
dec = sc.nextInt();
String bi = "";
while(dec > 0){
int rem = dec % 2;
// num *= 10;
// num += rem;
bi += Character.toString(rem + 48);
dec /= 2;

}
// if(num % 100 == 10){
// num = num/10;
// }
System.out.println("Binary Equivalent: " + new StringBuilder(bi).reverse().toString());

}
}

convert decimal to binary

Then use a string as follows:

   int num = scan.nextInt();

String s = "";
while (num != 0) {
int rem = num % 2;
num /= 2;
s = s + rem; // this concatenates the digit to the string in reverse order.

// if you want it in normal order, do it -> s = rem + s;
}
System.out.println(s);

How to Convert Decimal to Binary without arrays or recursion?

you declared binaryNum inside while loop so the scope of this variable will be inside the loop only, declare it outside the while loop and change binaryNum type to String

public class Decimaltobinary {

public static String decimalToBinary(int valueIn){

// String binaryOut = "";
// int counter = 0;
int remainder, i = 0;
String binaryNum ="";
while (valueIn != 0){
remainder = valueIn % 2;
valueIn /= 2;
binaryNum = remainder+binaryNum;
}
return binaryNum;
}

/**
* @param args the command line arguments
*/

public static void main(String[] args) {
// TODO code application logic here
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the decimal number: ");
int valueIn = keyboard.nextInt ();
String outputOut = decimalToBinary(valueIn);
System.out.println ("The output is: " +outputOut);
}

}

Converting an int to a binary string representation in Java?

Integer.toBinaryString(int i)


Related Topics



Leave a reply



Submit