Expand|Select|Wrap|Line Numbers
- public class Main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args){ //throws IOException
- // TO DO codestatic double toDecimal (String s)
- int dec;
- String bin, conv;
- int a=1;
- while (a==1)
- {
- //BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
- Scanner scan = new Scanner(System.in);
- System.out.println("Please enter decimal number to convert to binary" + "\n" + "maximum of 10 digits please");
- bin = scan.next();
- dec = Integer.parseInt(bin);
- int max[] = new int[10];
- int i = 0;
- if (dec/2!=0 || dec==1)
- {
- while(dec!=1)
- {
- max[i]=dec%2;
- dec = dec/2;
- i++;
- }
- max[i]=1;
- }
- System.out.print("The binary equivalent is " );
- for (int z=i; z>0; z--)
- {
- System.out.print(max[z]);
- }
- System.out.println("\n" + "Enter 1 for enter another value or 0 to exit:");
- String reply=scan.next();
- int b=Integer.parseInt(reply);
- if(b==1)
- a=1;
- else
- a=0;
- }
- }
- }