Hi,
I am the beginner in Programming, I want to convert binary numbers to decimals. I have made a program but don't know where I am wrong.
Please let me know, what's wrong in the coding !!
Code:
=============================================== while
/ / 12/31/00 Loop example 1 Java with
import java.lang .*;
import java.io. *;
public class Loop1 (
public static void main (String args []) throws IOException
(
int binary, temp, count, dec;
byte buffer [] = new byte [10];
binary = 0;
count = 1;
System.out.print ( "Enter 10 base decimal to convert to Binary:");
System.in.read (buffer);
String strInput = new String (buffer);
dec = Integer.parseInt (strInput.trim ());
temp = dec;
while ((dec / 2)! = 0) (
if ((dec% 2) == 1) (
binary = binary + count;
)
count = count * 10;
dec = dec / 2;
)
if (temp! = 0)
binary = binary + count;
System.out.println (temp + "Decimal equals to" + binary + "Binary digit");
)
)
============================================== do while
/ / 01/10/2001 Loop example 2 java with (do while)
import java.lang .*;
import java.io. *;
public class Loop2 (
public static void main (String args []) throws IOException
(
int binary, temp, count, dec;
byte buffer [] = new byte [10];
binary = 0;
count = 1;
System.out.print ( "Enter 10 base decimal to convert to Binary:");
System.in.read (buffer);
String strInput = new String (buffer);
dec = Integer.parseInt (strInput.trim ());
temp = dec;
do (
count = count * 10;
dec = dec / 2;
if ((dec% 2) == 1) (
binary = binary + count;
)
)
while ((dec / 2)! = 0);
if (temp! = 0)
binary = binary + count;
System.out.println (temp + "Decimal equals to" + binary + "Binary digit");
)
)
============================================== for
/ / 01/10/2001 Loop example 3 java with (for)
import java.lang .*;
import java.io. *;
public class Loop3 (
public static void main (String args []) throws IOException
(
int binary, temp, count, dec;
byte buffer [] = new byte [10];
binary = 0;
System.out.print ( "Enter 10 base decimal to convert to Binary:");
System.in.read (buffer);
String strInput = new String (buffer);
dec = Integer.parseInt (strInput.trim ());
temp = dec;
for (count = 1; ((dec / 2)! = 0); count = count * 10)
(
dec = dec / 2;
if ((dec% 2) == 1) (
binary = binary + count;
)
)
if (temp! = 0)
binary = binary + count;
System.out.println (temp + "Decimal equals to" + binary + "Binary digit");
)
)
Bookmarks