将十进制数转化为二进制数
程序员文章站
2022-03-08 15:09:33
...
将一个十进制数转化为二进制数
import java.util.Scanner;
public class TenToTwo{
public static void main(String[] args){
int i;
String str = "";
Scanner sc = new Scanner(System.in);
System.out.println("请输入要转化的数:");
int num = sc.nextInt();
while(num != 0)
{
i = num % 2;
num = num / 2;
str = i + str;
}
System.out.println(str);
}
}