下面以15为例子输出1111
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void decToBin(int n)//输入一个数15
{
int result = 0;//结果
int k = 1;
int i = 0;
while(n)//除到0就停止
{
i = n % 2; //余数i=1 //i=1 //i=1 //i=1
result = k * i + result; //result=1 //result=11 //result=111 //result=1111
k = k * 10; //k=10 //k=100 //k=1000
n = n / 2; //n=7 //n=3 //n=1 //n=0退出
}
printf("%d \n",result);//1111
}
int main() {
int n;
scanf("%d", &n);
decToBin(n);
}