เวลา post โค้ดให้ใช่แทคนี้นะครับจะได้ดูง่ายๆ
[src] ..your code ..[/src]
เวลา post โค้ดให้ใช่แทคนี้นะครับจะได้ดูง่ายๆ
[src] ..your code ..[/src]
Here is the code that transform number in decimal to binary and also count number of 1's.
This program use recursive method.
[src]
public class Binary
{
public static void Convert(int n)
{
if (n<=1)
System.out.print(n);
else
{
Convert(n/2);
System.out.print(n%2);
}
}
public static int count(int num)
{
if (num<=1)
return num;
if (num%2==1)
return count(num/2)+1;
return count(num/2);
}
public static void main(String[] args)
{
int num = 13;
System.out.print("=> You input "+num+" in decimal and in binary is :");
new Binary().Convert(num);
System.out.println("");
System.out.println("It has "+ count(num)+" of 1's");
}
}[/src]
[code]
public static void Convert(int n)
{
http://img98.imageshack.us/img98/731/ubd7518fj8.png
XSI - Behavior Programmer
Boardgame Thailand Fanclub
Ball Juggling~
Actions : (View-Readers)
There are no names to display.