编写程序,用递归法将一个十进制整数转换成二进制数。
发布网友
发布时间:2022-05-13 09:48
我来回答
共3个回答
热心网友
时间:2023-10-05 09:15
程序如下:
#include
#include
#define
N
100
char
a[N];
void
convert(int
n,int
i){
if(n>0){
a[i]=n%2;
a[i]+=48;
n/=2;i++;
convert(n,i);
}
}//十进制递归变二进制
void
main(){
long
int
n;
int
i,j,k,temp;
while(scanf("%d",&n)!=EOF){
for(i=0;i
=0;i--)printf("%c",a[i]);
printf("\n");
}
}
热心网友
时间:2023-10-05 09:16
#!/bin/sh
while :
do
echo "input a number:['q' to quit] \c"
read inputNumber
if [ "${inputNumber}" == "q" ]
then
exit 0
fi
bc<<!
ibase=10
a=${inputNumber}
obase=2
a+0
!
done
执行结果:
input a number:['q' to quit] 1
1
input a number:['q' to quit] 2
10
input a number:['q' to quit] 3
11
input a number:['q' to quit] 4
100
input a number:['q' to quit] 5
101
input a number:['q' to quit] 6
110
input a number:['q' to quit] 7
111
input a number:['q' to quit] 8
1000
input a number:['q' to quit] 9
1001
input a number:['q' to quit] 10
1010
input a number:['q' to quit] 1024
10000000000
input a number:['q' to quit] 4096
1000000000000
input a number:['q' to quit] 123425623454625646
110110110011111101111001101011010001100011010111101101110
input a number:['q' to quit] q
热心网友
时间:2023-10-05 09:16
程序如下:
#include<stdio.h>
#include<string.h>
#define N 100
char a[N];
void convert(int n,int i){
if(n>0){
a[i]=n%2;
a[i]+=48;
n/=2;i++;
convert(n,i);
}
}//十进制递归变二进制
void main(){
long int n;
int i,j,k,temp;
while(scanf("%d",&n)!=EOF){
for(i=0;i<N;i++)a[i]='\0';
convert(n,0);
i=N-1;
while(a[i]==NULL)i-- ;
for(;i>=0;i--)printf("%c",a[i]);
printf("\n");
}
}