C language 루프와 재귀함수를 각각 이용한 이진수 변환 2020/09/16 09:43 by The 2nd

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>

void print_binary_loop(unsigned long n);
void print_binary(unsigned long n);


int main()
{
unsigned long num = 254;

print_binary_loop(num);
print_binary(num);

return 0;

}

void print_binary_loop(unsigned long n)
{
int dap[1000];
int count = 0;

while (true)
{
int mok = n / 2;
int nam = n % 2;
n = mok;
dap[count] = nam;

if (mok == 0) {
dap[count + 1] = "\n";
break;
}
count++;
}
for(;count>=0;count--)
printf("%d",dap[count]);

printf("\n");
}

void print_binary(unsigned long n)
{
int mok = n / 2;
int nam = n % 2;
if (n>=2) print_binary(mok);

printf("%d", nam);
}


덧글

댓글 입력 영역