•

Check out our Community Blogs

# maher

Member Since 27 Oct 2011
Offline Last Active Jan 06 2013 06:19 AM

### In Topic: Decimal To Binary Converter

07 October 2012 - 09:03 AM

Hello
This is my very first tutorial on this forum
Converting Decimal to Binary is quite simple. This is how we do it :

```/**
* Converts Decimal To Binary
*
* @author Kushtrim
* @version 14.12.2011
*/
import javax.swing.*;
public class DecimalToBinary
{
public static void main(String[] args)
{
long d = new Long(JOptionPane.showInputDialog("Write the number in decimal form: ")).longValue();
String binary="";
for ( long decimal = d ; decimal > 0 ; decimal/=2 )
{
binary = decimal%2 + binary ;
}
JOptionPane.showMessageDialog(null,"Binary number:\n " + binary);
}

}```

The logic behind this is really simple, u just got to know a little math.,
To convert a number from decimal form ( base-ten , u know the system we normally use), eg. 4, u divide it by 2, and you get a reminder 0 or 1 ( in our case 4:2=2, remainder 0) ... We repeat this process(loop) until we get the quotient 0 (the remainder of 1:2=0 will be 1).
Then we read it from bottom to top ( from the last remainder).

Our example:
4:2=2 remainder 0
2:2=1 remainder 0
1:2=0 remainder 1

Decimal 4 = Binary 100

Feel free to ask if you didn't understand it:)

Note that there are built in methods in Java for this;)

```int decimal=415;
System.out.println(decimal + " in binary -> " + Integer.toString(decimal, 2));

String binary = "010110111";
System.out.println(binary + " in decimal -> " + Integer.parseInt(binary, 2));
```

both ways are good however I would prefer the built-in one.

### In Topic: Finding the nth Perfect Square

06 October 2012 - 04:12 AM

You're right . Thanks Orjan.

### In Topic: Finding the nth Perfect Square

06 October 2012 - 02:33 AM

we can simplify it to

```int num =1;
for(int i = 1; i <= 9 ; i++){
System.out.println("Perfect Square #" + i + ": " + num*num);
num++;
}
```

### In Topic: Java:Tutorial Setting up Java and What is an IDE?

06 October 2012 - 02:21 AM

that was helpful. Thanks. Keep up the good work

### In Topic: Finding the nth Perfect Square

05 October 2012 - 11:41 PM

now i got it. thanks a lot.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download