Description
将十进制整数n转换成二进制,并保存在字符数组中,最后输出。要求定义并调用convert()函数, 将十进制整数n对应的二进制数存入字符数组str中。
void convert(int n, char str[]);
Input
输入一个非负整数n,n<2^31。
Output
输出一个01字符串,即n对应的二进制数,不含前导0。输出占一行。
Sample Input
13
Sample Output
1101
HINT
Source
#include <stdio.h>
#include <stdlib.h>
void convert(int n, char str[]);
int main()
{
int n;
char str[10000];
scanf("%d",&n);
convert(n,str);
return 0;
}
void convert(int n, char str[])
{
int i=0;
int j=0;
if(n==0)
printf("0");
while(n>0)
{
str[i++]=n%2+'0';
n=n/2;
}
for(j=i-1;j>=0;j--)
printf("%c",str[j]);
}