Guess What this does, lol. It took me hours.
package dec2binconversion;
import java.util.Scanner;
/**
* @author Kris Gregg
*/
public class Dec2BinConversion
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner keys = new Scanner(System.in);
int dec;
String binary = "";
System.out.print("Enter Decimal Number: ");
dec = keys.nextInt();
for (int i = dec; i >= 1; i--)
{
if (dec % 2 != 0)
{
binary += '1';
}
else
{
binary += '0';
}
dec = dec / 2;
}//end of the Loop
for (int i = binary.length() - 1; i >= 0; i--)
{
if (binary.charAt(i) == '1')
{
//result = result + result.charAt(i);
System.out.print(binary.charAt(i));
}
else
//result = result + result.charAt(i);
{
System.out.print(binary.charAt(i));
}
}
}
}
No comments:
Post a Comment