I'm trying to make a java program where the user inputs a decimal number and it it then converted to binary. this is what i have so far but the while loop only loops once and only gives me 0 when i input 8. I know 8 in binary is 1000 so i don't understand what I'm doing wrong. I need the coding to be simple. please help thanks
import java.util.Scanner;
public class binary
{
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
int userNum = 0;
int binary = 0;
double newNum = 0;
int count = 0;
System.out.print("Enter a positive base 10 number: ");
userNum = scan.nextInt();
System.out.println();
for(; (Math.pow(2,count) <= userNum); count++)
{
}
while(!(count == 0))
{
if(userNum/Math.pow(2,count) != 0)
{
binary = userNum/(int)Math.pow(2,count);
System.out.print(binary);
}
userNum %= Math.pow(2,count);
count--;
userNum %= (int)Math.pow(2,count));
}
}
}
4 Answers 4
You shouldn't re-invent the wheel, use Integer.toBinaryString(int)
and something like,
Scanner scan = new Scanner(System.in);
System.out.print("Enter a positive base 10 number: ");
if (scan.hasNextInt()) {
int userNum = scan.nextInt();
System.out.printf("%d decimal is %s binary%n", userNum,
Integer.toBinaryString(userNum));
}
Also, Java classes should start with a capital letter. So your binary
should probably be Binary
. Finally, if you need to convert from binary to decimal, you can use Integer.parseInt(String, 2)
.
2 Comments
You can use the logic like below:
package com.java2novice.algos;
public class DecimalToBinary {
public void printBinaryFormat(int number){
int binary[] = new int[25];
int index = 0;
while(number > 0){
binary[index++] = number%2;
number = number/2;
}
for(int i = index-1;i >= 0;i--){
System.out.print(binary[i]);
}
}
public static void main(String a[]){
DecimalToBinary dtb = new DecimalToBinary();
dtb.printBinaryFormat(25);
}
}
Comments
You just got :
for(userNum = userNum; (Math.pow(2,count) <= userNum); count++)
{
}
Instead of:
for(userNum = userNum; (Math.pow(2,count) <= userNum); count++){
while(!(count == 0))
{
if(userNum/Math.pow(2,count) != 0)
{
binary = userNum/(int)Math.pow(2,count);
System.out.print(binary);
}
newNum = userNum%Math.pow(2,count);
count--;
userNum = (int)(newNum%(int)Math.pow(2,count));
}}
}
}
Basically you got empty if.
Comments
A couple issues:
Your count
is going to always start one too high, since the last part of the for
loop is going to be to increment it again.
You need the while
loop to run when count
is 0 to check the last bit.
The rest looks like it should be good, but I don't understand what the statements mean with newNum
.
An easier version of your algorithm is to use bitwise operators -- instead of having to divide by powers of two explicitly with Math.pow
, you can use bitwise operators to test if bits are on or off, and then print out 1's and 0's accordingly. For example:
public static void toBinary(int n) {
int mask = 1, nn = n;
while(nn != 0) {
mask << 1;
nn /= 2;
}
mask >>= 1;
while(mask != 0) {
System.out.print( (mask & n) ? "1" : "0" );
mask >>= 1;
}
}
EDIT: Also, your userNum = userNum
is redundant in the for
loop. Just use a null statement (;
) in the assignment part of the loop instead. So:
for(; (Math.pow(2, count) <= userNum); count++) {