Alright guys, here's my problem. I have this class that converts a decimal number into a binary value. However, the result is printed in reverse. I need help on making the binary value print out correctly. I was trying to use some code involving a lot of modulus work but it got way too confusing. Here's what I have.
CODE
//
//
import java.util.Scanner;
//begin class
public class DecimalToBinary
{
//begin main method
public static void main( String args[])
{
Scanner input = new Scanner( System.in); //creates Scanner object
//defines and initializes variables
int number = 0;
int counter = 1;
int p = 0;
int g = 0;
System.out.printf("Enter a decimal number:", number);//prompt
number = input.nextInt();
//begin while statement
while (number > 0)
{
if (number % 2 == 0)
{
p = 0;
System.out.printf("%d",p);
number = number / 2;
counter = counter * 10;
}
else
{
g = 1;
System.out.printf("%d", g);
number = (number - 1)/2;
counter = counter * 10;
}
}// end while statement
System.out.println();
}// end main method
}// end class
Now, this program is for a friend of mine and it needs to be worked on REALLY soon, as it is due on the 10th by midnight i believe.
I'm almost positive it's something simple. Don't be afraid to rework the program itself if need be, but make sure you only use beginner level work. I hear the teacher is quite fussy about stuff not covered.