Ex: 1001->0110.
It should work like this but doesn't . Please help me.
package Bynary;
import java.util.*;
import java.io.*;
public class Bynary
{
public static void main(String[] args)throws IOException
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.println("What do you want the program to do?");
System.out.println(" Decimal to binary ... 1");
System.out.println(" Binary to decimal ... 2");
String choice = input.readLine();
int c = Integer.parseInt(choice);
if(c==1)
{
System.out.println(" Enter a decimal number:");
String dba=input.readLine();
int db = Integer.parseInt(dba);
System.out.println(db);
int [] m = new int [10];
int j;
for(j=8;j>=1;j--)
{
m[j] = db%2;
db= db/2;
}
int sc = 0;
for(j=1;j<=8;j++)
{
if(m[j]==0)
{
if(sc==0)
{
continue;
}
}
System.out.print(+m[j] + " ");
sc = sc+1;
}
}
if(c==2)
{
System.out.println("Enter a binary number");
String bda=input.readLine();
int l=bda.length()-1;
double sum =0;
for (int i = 0; i <= bda.length(); i++)
{
switch(i)
{
case '0': bda = bda + "0";
break;
case '1': bda = bda + "1";
break;
default:
System.out.println("Thanks");
}
}
for(int w =0;w<bda.length();w++)
{
char r = bda.charAt(w);
String str = new Character®.toString();
int er = Integer.parseInt(str);
sum = sum + (er*Math.pow(2,l));
l--;
}
System.out.println("The decimal is : "+sum);
System.out.println("The decimal is : "+sum);
System.out.println("-" + sum);
input.readLine();
}
}
}

New Topic/Question
Reply




MultiQuote





|